By Webdrips


2016-04-09 05:18:47 8 Comments

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 comments

@NewZeal 2016-09-17 21:28:24

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();

@Stefanos Petrakis 2016-04-09 09:06:36

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!

@Webdrips 2016-04-09 09:55: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.

@Stefanos Petrakis 2016-04-09 10:04:58

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?

@Webdrips 2016-04-09 10:08:25

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.

@Stefanos Petrakis 2016-04-09 10:12:30

Your own implementation I reckon. Have you implemented/overriden the pluginTitle function then?

@Webdrips 2016-04-09 10:15:03

Correct it's a custom implementation, but no I did not implement/override pluginTitle()

@Webdrips 2016-04-09 10:21:58

I tried this for a view with two block displays, and indeed I got the annotation title "Block" instead of the custom display titles provided to each block display.

@Stefanos Petrakis 2016-04-09 10:26:45

Got it! Updating my answer!

@Webdrips 2016-04-09 10:40:17

Hmm now I'm getting an empty string. Again tried it with my custom display type and the blocks displays.

@Webdrips 2016-04-09 10:52:12

Note I am indeed getting a valid Drupal\views\Plugin\views\display\Block object from $displayObj = $view->getDisplay(); that includes the object Drupal\webprofiler\Views\TraceableViewExecutable, which in turn includes the display titles.

@Stefanos Petrakis 2016-04-09 10:54:37

Related Questions

Sponsored Content

1 Answered Questions

Entity reference view display is not displaying fields as expected

  • 2018-10-11 12:23:22
  • Elie Masaad
  • 426 View
  • 1 Score
  • 1 Answer
  • Tags:   views 8

1 Answered Questions

How to get view name and display ID in template_preprocess_node function

  • 2019-01-24 09:43:57
  • Kelvin Joysury
  • 607 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8 theming nodes

1 Answered Questions

[SOLVED] How to get the View Object from ViewsBlock?

  • 2019-03-30 14:48:43
  • Matt Campbell
  • 38 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8 blocks

1 Answered Questions

[SOLVED] Renaming the name of a field in a View

  • 2018-08-01 16:51:13
  • rike17
  • 35 View
  • 0 Score
  • 1 Answer
  • Tags:   views

1 Answered Questions

[SOLVED] How to change the image style used in a field in specific view results?

  • 2016-11-18 17:46:41
  • joekers
  • 319 View
  • 6 Score
  • 1 Answer
  • Tags:   views theming 8

1 Answered Questions

[SOLVED] Get display name from hook_preprocess_views_view

  • 2015-06-11 08:43:26
  • Chris
  • 5431 View
  • 7 Score
  • 1 Answer
  • Tags:   views hooks

1 Answered Questions

[SOLVED] Getting the uid from user name for a view argument

  • 2011-06-17 16:58:22
  • Arturo
  • 2682 View
  • 1 Score
  • 1 Answer
  • Tags:   users views

3 Answered Questions

[SOLVED] Views display settings are in the database but don't show up in views UI

  • 2012-03-22 20:51:47
  • user5072
  • 339 View
  • 1 Score
  • 3 Answer
  • Tags:   views 6 features

1 Answered Questions

[SOLVED] Printing the correct date value in a view

  • 2011-04-27 22:18:57
  • Joe
  • 1261 View
  • 2 Score
  • 1 Answer
  • Tags:   7 views datetime

Sponsored Content