On this page
Loading Algorithm
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.twigtemplate, hooks/files applying tonodewill apply first, following by hooks/files 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.
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 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:
MY_MODULE_preprocess_node($variables);MY_SECOND_MODULE_preprocess_node($variables);MY_MODULE/preprocessors/node.preprocessor.phpMY_SECOND_MODULE/preprocessors/node.preprocessor.phpMY_BASE_THEME_preprocess_node($variables);MY_CHILD_THEME_preprocess_node($variables);MY_BASE_THEME/preprocessors/node.preprocessor.phpMY_CHILD_THEME/preprocessors/node.preprocessor.phpMY_MODULE_preprocess_node__basic_page($variables);MY_SECOND_MODULE_preprocess_node__basic_page($variables);MY_MODULE/preprocessors/node--basic-page.preprocessor.phpMY_SECOND_MODULE/preprocessors/node--basic-page.preprocessor.phpMY_BASE_THEME_preprocess_node__basic_page($variables);MY_CHILD_THEME_preprocess_node__basic_page($variables);MY_BASE_THEME/preprocessors/node--basic-page.preprocessor.phpMY_CHILD_THEME/preprocessors/node--basic-page.preprocessor.php
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