0

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?

2
  • 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.
    – leymannx
    Commented Feb 21, 2022 at 19:42
  • 1
    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_()
    – Clive
    Commented Feb 21, 2022 at 20:04

1 Answer 1

0

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

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.