Drupal provides a lot of hooks that you can leverage to make it work exactly how you want. I just finished tracing how some theme preprocessing functions were being executed, and realized that there are some subtle assumptions which I hadn’t understood earlier. These are my notes about two specific preprocessing hooks and when they are executed, which is a valuable lesson learned after spending a day figuring out why some functions were being called twice, and why some variables where being overwritten or lost. There’s also confusion with the label ‘page’ because its both a preprocessing hook AND a default content type in Drupal.
Drupal provides two preprocessing hooks:
- hook_preprocess_page – for updating content on the final rendered page.
- hook_preprocess_node – for affecting how a node is rendered.
They each have very different assumptions and uses, so here are some guidelines
hook_preprocess_page can affect the layout and regions of the final webpage. If you need to display blocks or other content, in any region onthe webspage, use this. This is the function to use if you want to affect other regions in your layout based on the node that is being viewed.
hook_preprocess_node affects how an individual node is rendered. It known NOTHING about theme regions, and if you set them, they do not persist or get passed up to your page.tpl.php file (and descendants). Variables set here are only available in your node.tpl.php (or its offshoots). This is the function to use to change the layout within the main content region with a lot of control but, I’d emphasize, it can not change the contents of other template regions.