Score:1

Adding template suggestions for a view with display ID

jp flag

I need to have suggestions for views to create dedicated template. Actually I've this in my .theme:

function starter_d9_2022_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'views_view__' . $variables['view']->id();
}
function starter_d9_2022_theme_suggestions_views_view_unformatted_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'views_view_unformatted__' . $variables['view']->id();
}
function starter_d9_2022_theme_suggestions_views_view_list_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'views_view_list__' . $variables['view']->id();
}

This provide:

<!-- FILE NAME SUGGESTIONS:
   x views-view--acces-rubriques-2019.html.twig
   x views-view--acces-rubriques-2019.html.twig
   x views-view--acces-rubriques-2019.html.twig
   * views-view.html.twig
-->

File is working,but code inside doesn't display views content. Anyway, I would to use a more specific template, by display like:

views-view--acces-rubriques-2019--list--block-1.html.twig

How can I add this to my actual code ? Regards

Score:0
kz flag

This looks like a simple task. To achieve your desired template suggestion, you can add another suggestion that concatenates the display ID of the view. Here's how you can modify your code:

function starter_d9_2022_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  $view = $variables['view'];
  $suggestions[] = sprintf('views_view__%s', $view->id());
  // Add a more specific template suggestion for the list display.
  $suggestions[] = sprintf('views_view__%s__%s', $view->id(), $view->getDisplay()->display['id']);
}


function starter_d9_2022_theme_suggestions_views_view_list_alter(array &$suggestions, array $variables) {
  $view = $variables['view'];
  $suggestions[] = sprintf('views_view_list__%s', $view->id());
  // Add a more specific template suggestion for the list display.
  $suggestions[] = sprintf('views_view_list__%s__%s', $view->id(), $view->getDisplay()->display['id']);
}

The template suggestion will follow the pattern views-view--<view_id>--<display_id>.html.twig and views-view-list--<view_id>--<display_id>.html.twig as example.

Don't forget to clear the Drupal cache after making these changes so that the new template suggestions can take effect.

I hope this helps!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.