Logo

Edward Ford

  • Archive
  • RSS
  • Ask me anything

Drupal 6 - Page template variables in nodes

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.

    • #Drupal
    • #Page template variables
  • 3 years ago
  • 3
  • Comments
  • Permalink
  • Share
    Tweet

3 Notes/ Hide

  1. edwardford posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →

Edward Ford

About

Freelance Digital Marketing Consultant & Web Developer venturing around the world on route to San Francisco.

See my portfolio site

Me, Elsewhere

  • @edward_ford on Twitter
  • My Skype Info
  • Linkedin Profile

Twitter

loading tweets…

Following

I Dig These Posts

See more →
  • Post via aslakhellesoy
    Cucumber 1.0.0

    Cucumber

    I am so thrilled about this I set up a blog!

    Today I released Cucumber 1.0.0 after three years in the making. Here are...

    Post via aslakhellesoy
  • Photo via theartofanimation

    Yun Byoung Chul PENE MENN

    Photo via theartofanimation
  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr