Monday, March 29, 2010

Drupal 6 - How to embed a region in a node

Drupal 6 - How to embed a region in a node
Submitted by tom on Mon, 2008-08-04 03:22
There will come a time when you'll want to add a new region to your Drupal theme, weather you are using a stock theme, a modification of a base theme such as Zen (like me) or creating your own theme from scratch.

Drupal 6 makes the creation of new regions as simple as adding one line of code to your themes template.php file. However, by default new regions created in this manor will only be available to your themes page template (page.tpl.php) and a little extra work will be needed if you want to display it somewhere else.


For the purposes of this article, lets say that we want to add a new region at the top of all story nodes for displaying some google adsense adverts. But, we only want the region to be displaed if we are in the full node view and not in teaser mode.

Add the region to your themes .info file
First thing to do is to define the new region. So, open up your themes .info file (it should be named yourtheme.info) and look for the lines which define the regions. In the Zen theme, these lines look like this:

view sourceprint?

<?php
 regions[left]           = left sidebar  

 regions[right]          = right sidebar  

 regions[navbar]         = navigation bar  

 regions[content_top]    = content top 

 regions[content_bottom] = content bottom  

 regions[header]         = header  

 regions[footer]         = footer  

 regions[closure_region] = closure 
?>

The name in the square brackets is the name of the variable that will be made available to your templates. The text after the equals sign is a just descriptive text that will be used on the admin/build/blocks page. Choose a name which reflects what the region will be used for so you can easily identify it later - I chose the name node_advert_top which will make a variable named $node_top_advert available to my templates. To add the new region, simply add a new line under the existing region declarations.

view sourceprint?
<?php
 regions[footer]         = footer  

 regions[closure_region] = closure  

 regions[node_advert_top]= node advert top 
?>
Expose the region variable to your node template file.
By default, region variables are available for use in your page template file (page.tlp.php

). However, we want to display our advertisements block within the actuall node itself so we need to add the new region variable $node_advert_top to the node.tlp.php file. To do this we can use the preprocess_node() function. Open up your template.php file, create a function named yourtheme_preprocess_node() and add your new region to the $vars variable.

view sourceprint?
<?php
 function kirkdesigns_preprocess_node(&$vars, $hook) {  

   $vars['node_advert_top'] = theme('blocks', 'node_advert_top');  

 } 
?>
Place the region where you want it.
Now you just need to open up your node template file (node.tlp.php) and add the region where you would like it to appear. Remember though, I only want to display this advert block on story nodes, and I only want it to be displayed if we are viewing the full article. The first if statement does exactly that.

view sourceprint?
<?php
 if ($node_advert_top && !$teaser && $node->type == 'story'): ?>


<?php print $node_advert_top;
?>



<?php
endif;
?>

No comments: