1

I have some data to be extracted from external sources. I want to display those data in a custom template tpl page ( page--zzz.tpl.php ) . I had implemented hook_node_view in my module, and with the node->nid, output the data to be displayed on a node.tpl.php page.

However, the designer wanted to use the full width of the page, so I had to display the collected data in the page-zzz.tpl page, instead of just in the node.tpl.php, so I have moved my function for collecting the data from my module to the template.tpl.php, and inside the function template_preprocess_page, with the node->nid, I collect the same data.

Is that correct? Everything would have been much easier if I had been able to access to the $page variable inside of the hook_node_view function. I tried but I did not succeed!!!

Please advice me. Now it is working but I am hesitating whether there was need to move the collecting data function from my module to the template tpl page.

1

hook_node_view() is not really suitable for loading anymore data. There is a hook_node_load() hook, which you'll receive an array of all nodes that site is about to load. Note that this is only for loading, and that doesn't mean the user is on a node/XYZ page. You will probably need to add a URL argument match to make sure you load only the data related to the given node. Otherwise, pull all the information at once, and store them in each $node variable, which is passed by reference (all PHP 5 objects are).

The reason to use node_load hook over node_view hooks is that all modules implementing preprocess and view hooks now have access to this new piece of node. Otherwise, a hook_node_view is OK.

Secondly, you do not need to load them again in hook_preprocess_page at all. For Drupal 7 and later, hook_page_alter() is recommended. Since you store the data in hook_node_view/load hook, simply place the content in the regions you need. If this is theme specific, hook_preprocess_page() is better, but most of the contribs that take over entire page contents tend to use hook_preprocess_page(). Besides, you will have access to the renderable array because other preprocess functions are not executed at that moment.

You can get the current node like below (make sure you add arg() conditions to check the page router is node/%node)

$node = menu_get_object();

This $node object will contain the data you stored in the load/view hook. Make sure you pass the first argument to hook_page_alter(&$build) by reference, and place the content anywhere you want like god.

| improve this answer | |
  • Thanks for your detailed reply, that I will study carefully. However, I thought that the region was exclusive of $page, i. e., print render ( $page[name_of_region] ) would print the region, I didn't know that it was accessible through $node. – Cesar Jan 8 '15 at 17:26
  • Store raw data in the node_load hook. Process them in the page_alter hook. – AyeshK Jan 8 '15 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.