4

I'm trying to create a custom template for a view page in Drupal 8.3.x. It's going in a custom theme, and needs to be a view-level template (so not page- or node-).

I've been fighting with it for a while and figured it might be an issue with the specific name, so I've made the code as sparse as possible - still no luck.

I have the following code in mytheme.theme:

function mytheme_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'test_template';
}

And the following file structure:

- mytheme
  ~ templates
    * test-template.html.twig
  ~ mytheme.theme
  ~ mytheme.info.yml
  ~ ... etc.

I know that the theme_suggestions alter function is working properly; I have Twig debugging enabled, and when I pull up my site and look at the code, I can see the following: (note the "X" next to views-view.html.twig showing that as the currently used Twig file)

<!-- FILE NAME SUGGESTIONS:
   * test-template.html.twig
   x views-view.html.twig
-->

So it's adding the suggestion. And the template file is named correctly. I know it's not a directory issue, because I have other .html.twig files in that /templates/ folder that are working correctly.

Does anyone know why the file name suggestion is working, but the matching file isn't?


Edit: Kevin pointed out a possible duplicate ticket in the comment below. That's only half-related. It does solve the symptom in this particular example - I have been able to pull in one of the templates mentioned here, and it works for this case.

However, the title of the ticket and general issue still holds. I can't get the hook_theme_suggestions_hook_alter() to work with my custom theme_suggestion / template. While it turns out the view I used as my example could be solved another way, there are other instances where that won't be the case.

3
  • Possible duplicate of Create a template for a specific view
    – Kevin
    Jun 7, 2017 at 21:41
  • I think the linked question here can help. You don't necessarily need to invent new naming suggestions, there should be a handful to pick from. They're just not easy to come across.
    – Kevin
    Jun 7, 2017 at 21:41
  • @Kevin - In this case, I do need a new naming suggestion. I've been through the default suggestions, and there is no view-level default naming suggestion that pulls in the specific view ID. If you know of one I haven't found, I'd be happy to use it, but everything I've searched has turned up no default options. Thus, the hook_alter function above.
    – Lauren G
    Jun 8, 2017 at 1:50

1 Answer 1

6

Found the solution. As it turns out, for some reason Drupal doesn't play nicely with custom theme suggestions starting with a word + single dash.

I updated my HOOK_theme_suggestions_HOOK_alter() function to include $suggestions[] = 'test__template'; (with 2 underscores between the words) and updated my template name to test--template.html.twig (with 2 dashes to match the 2 underscores), and everything works as expected.

1
  • Perfect. Good question. Good answer. For others, just to be crystal clear, you need to replace all hyphens in the suggestion to underscores.
    – Jeff
    Mar 21, 2018 at 17:05

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.