11

I can load a view with $view = Views::getView('my_view_id');, but how do I use that object to load the display_title or say an array containing information including the display name?

Note: This is not to be confused with loading the display's title that might be used when printing the view, as there's a method for that (getTitle()).

2 Answers 2

19

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!

10
  • Darn I was missing the last line...thanks for that. I'm currently getting the annotated title for my custom display plugin, so perhaps I'm missing some setting in my display plugin.
    – Webdrips
    Commented Apr 9, 2016 at 9:55
  • Hey there. So, what do you get exactly? What do you mean the annotated title? From the pluginTitle function, I got a TranslatableMarkup object, are you getting something else there? Commented Apr 9, 2016 at 10:04
  • I have a custom views display plugin as described here: api.drupal.org/api/drupal/…. It is annotated with @ViewsDisplay, which includes a title. That title is what I am getting back with the last line.
    – Webdrips
    Commented Apr 9, 2016 at 10:08
  • Your own implementation I reckon. Have you implemented/overriden the pluginTitle function then? Commented Apr 9, 2016 at 10:12
  • Correct it's a custom implementation, but no I did not implement/override pluginTitle()
    – Webdrips
    Commented Apr 9, 2016 at 10:15
0

The following code depends on the ctools_views module.

$view = \Drupal\views\Views::getView('your_view_id');
$view->setDisplay('your_display_id');
$displayObj = $view->getDisplay();
$displayName = $displayObj->display['display_title'];

To get the display title only requires the following code.

$view = \Drupal\views\Views::getView('your_view_id');
$view->setDisplay('your_display_id');
$displayName = $view->getTitle();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.