Score:0

Cannot get hook for custom twig template to work

cn flag

I have a custom template with the following twig file:

views-view-unformatted--offices_contact_us_page_.html.twig

The view is added as part of extra fields module inside of a Drupal paragraph. I have tried everything but simply cannot get the hook to work, however, the .theme file is loaded correctly as I can add an exit to the preprocess_page() function and that works.

I have tried:

  1. _preprocess_views_view_unformatted()
  2. _preprocess_views_view_unformatted__offices_contact_us()
  3. _proprocess_node()
  4. _preprocess_views()

I am able to inject my variables through the _preprocess_page, but then the variable is not available inside the above view, so need to alter that hook. Any ideas as to what I am doing wrong or alternatively is there a way for me to "print" all hooks that are loaded (like their names) during the request so that I can better determine what I am doing wrong?

leymannx avatar
ne flag
Template names normally don't contain underscores. Also not in the end. If you put `<h1>Foobar</h1>` into the template does it get printed or not? Have you enabled Twig debugging? There you'll see next to the template suggestions which hook you can hook in. Where exactly did you place the hooks? And how are they named exactly? Please update your question.
cn flag
If the view does have a trailing underscore in the name, the preprocess would need to match, e.g. `_preprocess_views_view_unformatted__offices_contact_us_()`
Score:0
ru flag

you can try to debug a possible hook name. To do this, you should enable Twig templates debugging. You can enable it in a next way:

  1. Open the sites/default/services.yml file. If you don't have it - copy sites/default/default.services.yml into sites/default/services.yml.
  2. Find twig.config -> debug parameter, set it as 'true', and save the file.
  3. Clear caches and refresh the page.

Then, please open the browser inspector, and try to find the comment to your template file. You'll see something like this: enter image description here THEME HOOK from the comment is the correct one for debugging.
So your preprocess function name should consist of: theme name, "preprocess" word + theme hook from the comment:

THEME_NAME_preprocess_THEME_HOOK
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.