Loading Algorithm

Last updated on
29 March 2025

Algorithm Summary

  • Module Hooks, and as such Module Preprocessor Plugins, 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/plugins applying to node will apply first, following by hooks/plugins 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.
  • Plugins will always be executed before traditional THEME hook functions.
    • This is because plugins are considered a more "Back-end" solution. As such, tasks done in plugins happen before the theme's frontend layer. This is why the theme plugins run before theme traditional hooks
  • Plugins will always be executed AFTER traditional MODULE hook functions.

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 Plugins follows these rules closely. They are programmed to act between the module layer and the theme layer. If you consider that you have equivalent Preprocessor Plugins 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/src/Plugin/preprocessors/NodePreprocessor.php
  4. MY_SECOND_MODULE/src/Plugin/preprocessors/NodePreprocessor.php
  5. MY_BASE_THEME/src/Plugin/preprocessors/NodePreprocessor.php
  6. MY_CHILD_THEME/src/Plugin/preprocessors/NodePreprocessor.php
  7. MY_BASE_THEME_preprocess_node($variables);
  8. MY_CHILD_THEME_preprocess_node($variables);
  9. MY_MODULE_preprocess_node__basic_page($variables);
  10. MY_SECOND_MODULE_preprocess_node__basic_page($variables);
  11. MY_MODULE/src/Plugin/preprocessors/BasicPageNodePreprocessor.php
  12. MY_SECOND_MODULE/src/Plugin/preprocessors/BasicPageNodePreprocessor.php
  13. MY_BASE_THEME/src/Plugin/preprocessors/BasicPageNodePreprocessor.php
  14. MY_CHILD_THEME/src/Plugin/preprocessors/BasicPageNodePreprocessor.php
  15. MY_BASE_THEME_preprocess_node__basic_page($variables);
  16. MY_CHILD_THEME_preprocess_node__basic_page($variables);

Help improve this page

Page status: No known problems

You can: