On this page
Loading Algorithm
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.twigtemplate, hooks/plugins applying tonodewill apply first, following by hooks/plugins applying tonode__basic_page/node--basic-page.
- For the
- 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:
MY_MODULE_preprocess_node($variables);MY_SECOND_MODULE_preprocess_node($variables);MY_BASE_THEME_preprocess_node($variables);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:
MY_MODULE_preprocess_node($variables);MY_SECOND_MODULE_preprocess_node($variables);MY_BASE_THEME_preprocess_node($variables);MY_CHILD_THEME_preprocess_node($variables);MY_MODULE_preprocess_node__basic_page($variables);MY_SECOND_MODULE_preprocess_node__basic_page($variables);MY_BASE_THEME_preprocess_node__basic_page($variables);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:
MY_MODULE_preprocess_node($variables);MY_SECOND_MODULE_preprocess_node($variables);MY_MODULE/src/Plugin/preprocessors/NodePreprocessor.phpMY_SECOND_MODULE/src/Plugin/preprocessors/NodePreprocessor.phpMY_BASE_THEME/src/Plugin/preprocessors/NodePreprocessor.phpMY_CHILD_THEME/src/Plugin/preprocessors/NodePreprocessor.phpMY_BASE_THEME_preprocess_node($variables);MY_CHILD_THEME_preprocess_node($variables);MY_MODULE_preprocess_node__basic_page($variables);MY_SECOND_MODULE_preprocess_node__basic_page($variables);MY_MODULE/src/Plugin/preprocessors/BasicPageNodePreprocessor.phpMY_SECOND_MODULE/src/Plugin/preprocessors/BasicPageNodePreprocessor.phpMY_BASE_THEME/src/Plugin/preprocessors/BasicPageNodePreprocessor.phpMY_CHILD_THEME/src/Plugin/preprocessors/BasicPageNodePreprocessor.phpMY_BASE_THEME_preprocess_node__basic_page($variables);MY_CHILD_THEME_preprocess_node__basic_page($variables);
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion