Featured Posts

Zend Cache Error - Silverstripe 2.4 admin After uploading a new site built on Silverstripe 2.4 to my shared host, the following error reared it's head when trying to access the admin area. [User Error] Uncaught Zend_Cache_Exception: cache_dir...

Read more

Allowed memory size error in Silverstripe Silverstripe have made great improvements in the memory consumption in version 2.4 but the admin area can still get the frustrating error message. Fatal error: Allowed memory size of 268435456 bytes...

Read more

MailChimp - Usability at its very best With it's funky post monkey mascot, the MailChimp website puts a smile on your face whenever you visit. Not only do MailChimp paint a pretty picture but their website and online service is one of the...

Read more

Web Development Frameworks After spending time with retro coded content management systems like Drupal, I started getting frustrated with developing.  I started reading about web development frameworks on blogs like Sitepoint &...

Read more

Drupal Add classes and id to body tag Drupal body class settings can be customised easily with a quick tweek of the template file. One of the most important class/id elements in CSS is the one defined in the body.  It allows for the...

Read more

twitter

Follow on Tweets

  •  

Drupal 6 – Page template variables in nodes

Category : Coding

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.