Drupal - How can I get a display name from the view ID and display ID?

After reading throug the docs (https://api.drupal.org/api/drupal/core!modules!views!src!ViewExecutable.php/class/ViewExecutable/8), I tried the following (which worked):

$view = \Drupal\views\Views::getView('your_view_id');
$view->setDisplay('your_display_id');
$displayObj = $view->getDisplay();
//$displayPluginTitle = $displayObj->pluginTitle()->render();
//$displayTitle = $displayObj->getOption('title');
$displayName = $displayObj->display['display_title'];

N.B.1: I use the render function on a TranslatableMarkup object, this is what the pluginTitle function returns. It could be possible to use other functions there, like, e.g. getUntranslatedString
N.B.2: I misunderstood the OP. :-) Commented out the faulty part of the code, which returns the display plugin's title, and not the display's title as requested. N.B.3: I misunderstood the OP. AGAIN! :-) Commented out the faulty part of the code, which returns the title of the display, and not the display's name as requested. To my defense, that property is labeled 'Display name' and its name 'display_title' (as was mentioned in the OP).

Screenshot from Views example:

enter image description here

And a test script that gets the title of the display (with the output):

enter image description here

Cheers!

Tags:

Views

8