I currently have two templates, one of which is page.html.twig, which is the main page and I can preprocess with MYTHEME_preprocess_page(). MYTHEME_preprocess_page() affects all the other pages in different modules.

The other template is _carousel.html.twig (theme/templates/includes/_carousel), which I include into page.html.twig, but I cannot hook into it with MYTHEME_preprocess__carousel().

Is this the default behavior, or am I missing something?

  • 1
    Yes, preprocess_page affects all pages, because that is the rendering stack. HTML, Page, Region, blocks, etc. Its hard to tell what you are trying to do here without more information.
    – Kevin
    Sep 21, 2022 at 17:18
  • @Kevin I guess the question is why MYTHEME_preprocess__carousel() cannot be used for a template file that is included into another template file.
    – apaderno
    Sep 21, 2022 at 20:51
  • Hey @Kevin, sorry for not answering earlier. What i'm trying to do is more of a reusable component approach. Since i need the same carousel on another page, I want to fetch the data only when it's included, not on every page.
    – murko233
    Sep 22, 2022 at 11:52

1 Answer 1


hook_preprocess_page() is invoked for every page Drupal renders. That's why it needs to check for which page is invoked, if it really needs to preprocess specific pages.
A preprocess hook isn't invoked for template files that are included from other template files.

For both the cases, the answer is yes, that is default, expected behavior.

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.