Loading Algorithm

Last updated on
29 March 2025

Algorithm Summary

  • Preprocessor Files will always execute after traditional hooks if both exist.
  • Module Hooks, and as such Module Preprocessor Files, will always execute first.
  • If a theme hook has a base hook, all base hook preprocessing will happen before preprocessing for the child hook.
    • For the node--basic.html.twig template, hooks/files applying to node will apply first, following by hooks/files applying to node__basic_page/node--basic-page.
  • If a child theme has a base theme, all preprocessing from the base theme will apply to templates inherited by the child theme. This rule goes all the way up to the root theme in a dependency chain.

Explanation & Illustration

To understand the loading algorithm, we must first review how Drupal traditionally loads preprocess functions.

To illustrate, consider the following preprocess functions applying to a node.html.twig template:

  • MY_MODULE_preprocess_node($variables);
  • MY_SECOND_MODULE_preprocess_node($variables);
  • MY_BASE_THEME_preprocess_node($variables);
  • MY_CHILD_THEME_preprocess_node($variables);

Drupal will first apply preprocess hooks from modules. Then, after all module hooks have been processed, theme hooks will apply.

Assuming that MY_CHILD_THEME has its base theme set to MY_BASE_THEME, Drupal will also make sure to process all base theme hooks before processing the child theme hooks.

Therefore, the hook functions you see above will be executed in this order:

  1. MY_MODULE_preprocess_node($variables);
  2. MY_SECOND_MODULE_preprocess_node($variables);
  3. MY_BASE_THEME_preprocess_node($variables);
  4. MY_CHILD_THEME_preprocess_node($variables);

Adding suggestions into the mix, consider the following new list of functions:

  • MY_MODULE_preprocess_node($variables);
  • MY_MODULE_preprocess_node__basic_page($variables);
  • MY_SECOND_MODULE_preprocess_node($variables);
  • MY_SECOND_MODULE_preprocess_node__basic_page($variables);
  • MY_BASE_THEME_preprocess_node($variables);
  • MY_BASE_THEME_preprocess_node__basic_page($variables);
  • MY_CHILD_THEME_preprocess_node($variables);
  • MY_CHILD_THEME_preprocess_node__basic_page($variables);

If we're in a node--basic-page.html.twig template, then the node__basic_page hook functions are added to the mix. Drupal, in this case, will process all base hooks before processing the suggestion hooks.

Therefore, the functions will execute in the following order:

  1. MY_MODULE_preprocess_node($variables);
  2. MY_SECOND_MODULE_preprocess_node($variables);
  3. MY_BASE_THEME_preprocess_node($variables);
  4. MY_CHILD_THEME_preprocess_node($variables);
  5. MY_MODULE_preprocess_node__basic_page($variables);
  6. MY_SECOND_MODULE_preprocess_node__basic_page($variables);
  7. MY_BASE_THEME_preprocess_node__basic_page($variables);
  8. MY_CHILD_THEME_preprocess_node__basic_page($variables);

Now with all of this in mind: Preprocessor Files will always execute AFTER traditional hooks, and follow the exact same rules as above. If you consider that you have equivalent Preprocessor Files for each of the functions above, you can expect the following order of execution:

  1. MY_MODULE_preprocess_node($variables);
  2. MY_SECOND_MODULE_preprocess_node($variables);
  3. MY_MODULE/preprocessors/node.preprocessor.php
  4. MY_SECOND_MODULE/preprocessors/node.preprocessor.php
  5. MY_BASE_THEME_preprocess_node($variables);
  6. MY_CHILD_THEME_preprocess_node($variables);
  7. MY_BASE_THEME/preprocessors/node.preprocessor.php
  8. MY_CHILD_THEME/preprocessors/node.preprocessor.php
  9. MY_MODULE_preprocess_node__basic_page($variables);
  10. MY_SECOND_MODULE_preprocess_node__basic_page($variables);
  11. MY_MODULE/preprocessors/node--basic-page.preprocessor.php
  12. MY_SECOND_MODULE/preprocessors/node--basic-page.preprocessor.php
  13. MY_BASE_THEME_preprocess_node__basic_page($variables);
  14. MY_CHILD_THEME_preprocess_node__basic_page($variables);
  15. MY_BASE_THEME/preprocessors/node--basic-page.preprocessor.php
  16. MY_CHILD_THEME/preprocessors/node--basic-page.preprocessor.php

Help improve this page

Page status: No known problems

You can: