In a recent project I discovered a major WTF in the theme hook suggestions. Perhaps someone could clarify this.
I needed to distiguish between views block and views page and added the following hook:
/**
* Implements hook_theme_suggestions_HOOK_alter() for node templates.
*/
function MY_THEME_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
$view = $variables['view'];
$suggestions[] = 'views__' . $view->getDisplay()->getBaseId();
}
This ends up in the following suggestions:
<!-- FILE NAME SUGGESTIONS:
* views--block.html.twig
x views-view.html.twig
-->
I added the template called views--block.html.twig but the template does not get discovered.
The theme system still uses views-view.html.twig.
Now the WTF:
If i rename the file from views--block.html.twig to views-view--block.html.twig there are new suggestions added without changing the preprocess function:
<!-- FILE NAME SUGGESTIONS:
x views-view--block.html.twig
* views--block.html.twig
x views-view--block.html.twig
* views-view.html.twig
-->
And now it works. For me this is not understandable, could someone explain this to me ?
Comments
Comment #2
yobottehg CreditAttribution: yobottehg commentedtypos
Comment #3
googletorp CreditAttribution: googletorp as a volunteer and at Reveal IT commentedThis seems a bit weird, but the suggestion you made originally is "wrong" in the sense that it should be "base_template_name__one_or__more-sugestions"
You can make whatever suggestions you like, but Drupal can only find templates that are registered. It will automatically find variants of an original template. Since there is no views.html.twig template/theme function it wont find your views--block.html.twig template.
So most of this is expected.
The behavior after adding the "correct" template seems a bit weird, there might be a bug somewhere or views might be doing some magic with template suggestions.
Comment #4
star-szrI would suggest trying some or all of this again with the latest patch from #2118743: Twig debug output does not display all suggestions when an array of theme hooks is passed to #theme applied. In this case views does do some things differently.
Comment #5
xjmThese can be confusing for sure. This isn't an actual bug though, so moving to support request. Testing #2118743: Twig debug output does not display all suggestions when an array of theme hooks is passed to #theme sounds like a good idea. We could also have a task to improve the documentation, since it's a recurring DX/TX frustration.
Comment #16
cilefen CreditAttribution: cilefen commentedI am closing this support request because there have been no recent comments.