6

I'm adding in some theme suggestions using mytheme_theme_suggestions_alter(), when certain criteria is met. My issue is, for some reason these additional theme suggestions are appearing at the top of the theme suggestions list whilst debugging.

Basically my theme suggestion is ignored, as Drupal believes my additional theme suggestions aren't a priority over the default ones.

<!-- FILE NAME SUGGESTIONS:
   * views-view--venues-grid-rows.html.twig
   x views-view.html.twig
-->

I've tried some 'dirty' solutions, such as trying to clear or reverse the $suggestions[] array without any luck.

My guess is that the hook I'm using is actually running too early, however I'm unable to find a more appropriate one. Something dedicated to hooking into view theme suggestions would be great.

Thanks in advance.

7
  • How does it know to use this template? Does the file name have typo(s)? Can you post that code?
    – Kevin
    Commented Feb 7, 2017 at 16:30
  • I don't understand what you're trying to say, sorry. As you can see in the HTML debugging comment above, my theme suggestion is there, however it's choosing to load the default views-view.html.twig. Commented Feb 7, 2017 at 16:33
  • I see that it's there, what I am saying is does the twig template have a typo preventing it from being picked up?
    – Kevin
    Commented Feb 7, 2017 at 16:34
  • Ah, I see. And yep, I have a file named views-view--venues-grid-rows.html.twig. Commented Feb 7, 2017 at 16:37
  • 1
    @Kevin I have encountered the same issue as the question poster - and unfortunately, changing the module weight is still not changing the order of the theme suggestions (I've tried values both higher and lower than the View module, to be sure). Is there anything else that can be tried?
    – user34185
    Commented Feb 9, 2017 at 16:20

1 Answer 1

1

You shouldn't need to add a theme suggestion if you're trying to override the view template for a given view ID. The confusing thing is the fact that Views' suggestions don't show in the debug code (see: https://www.drupal.org/node/2118743).

If you add a file named views-view--view-machine-name.html.twig (replacing machine-name, of course) in your theme's templates directory and clear caches, it should pick it up. Note: it has to be templates and not in your theme's root or my_templates or anything else. Drupal is picky that way.

You should not need to worry about module/theme weights as all module alter functions run before theme alter functions.

Finally, if you are adding theme suggestions, I would recommend using the more targeted hook_theme_suggestions_HOOK_alter. (For views, the HOOK is views-view so your function would look like:

function mytheme_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  $suggestions[] = ...
}

Hope that helps.

Your Answer

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

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