1

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

1 Answer 1

0

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!

Your Answer

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

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