I have just completed working on a Drupal project that required a slightly different coding practices to the normal Drupal theme. In order to meet the clients design, the inside pages needed to have the 2nd level of the primary menu defined inside the node.
Normaly, the primary menu is defined in page.tpl.php using:
<?php
print theme('links', $primary_links,array('class'=>'links primary-links'));
?>
but this is restricted to the variables in page.tpl.php and not accessible in node.tpl.php. Creating the link is actually very simple.
Out of the box, Drupal has two type of theme variables, “Page” and “Node” variables. A complete list of available variables can be found at Matt Vance’s Minezone blog. A handy trick when debugging the variables in your page and node template is to use Drupal’s get_defined_vars(); function at the bottom of the template files. I wrap them in a quick if statement so i can switch them on when required.
Node.tpl.php:
<?php
print '<pre>';
if ($_GET['do'] == 'node-att') {
print_r(get_defined_vars());
}
print '</pre>';
?>
page.tpl.php
<?php
print '<pre>';
if ($_GET['do'] == 'page-att') {
print_r(get_defined_vars());
}
print '</pre>';
?>
Now we can see all our available variables we can add some new ones. Drupal 6 handles the creation of theme variables slightly different than Drupal 5 with the introduction of the preprocess functions. To keep this post simple, we are only going to look at no.8, “engineName_preprocess_hook”.
The preprocess function is new to Drupal 6 and basically does what it says on the tin. It is preprocessed to setup variables to be available when rendering the template.
For our situation, we want to use phptemplate_preprocess_node to hook the node:
function phptemplate_preprocess_node(&$vars, $hook) {
$tree = menu_navigation_links('primary-links', $level = 1);
$vars['node_menu'] = theme('links',$tree);
}
Inside our function, we have called menu_navigation_links to return an array of the primary links. In this case, I only required the sub menu items for the current page so we pass $level=1 to say where we would like the menu to start from. Remember this is an array so if you want to start with the top level menu items, use $level=0.
Our 2nd line defines the template variable node_menu that we theme the primary links array into links.
The final piece of the puzzle is to call the variable in your node.tpl.php.
<?php print $node_menu; ?>
And voila, you have a menu in your node. It is simple to do but I could not find any documents related to it so hopefully this will help you. There is so much more that can be done with the preprocess function, this is just the tip of the iceburg.






