We help forward-thinking leaders design, build, and launch exceptional digital solutions through a blend of AI, design, and technology.

Adding a Region to the Node Template

Drupal logo banner

Typically, regions are returned in a theme’s page.tpl.php file. This works for almost everything, but as we found recently, there are situations where a region might need to be made available to the node.tpl.phpfile for the purpose of more specifically control a block’s location within a node. A good example of this is a node that has comments enabled. The node content and its comments will appear one after the other, both being returned by the value $content in the page.tpl.php. How to add a block between them?

First, we want to prepare the theme to accept our node-based region. This will involve defining a new region, overriding the default node.tpl.php file, and creating a theme override. Then, it’s just an issue of modifying the node template to print the region correctly.

Define the region

In your theme, you should find a .info file. This file exists to identify the theme to Drupal, as well as to provide you a way of globally including any regions, css or javascript. The top of the file should look something like this:

name = My Theme

description = A description of my theme.

screenshot = images/screenshot.png

core = 6.x

engine = phptemplate

Just below this, add the following:

; Regions

regions[node_bottom] = Node Bottom

… and save the file.

Override the default node template

Navigate inside your Drupal directory, and copy /modules/node/node.tpl.php into your theme directory.

Overriding the default node template in Drupal

Make the region available to the node template

Regions, by default, can only be returned from the page.tpl.php. This is easy enough to fix. Open your theme’s template.php file (create it if it doesn’t exist), and add this to it:

function YOURTHEME_preprocess_node(&$vars) {

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

}

Rebuild the theme registry

Most of the changes you’ve made so far will not be automatically recognized by Drupal. You’ll have to rebuild the theme registry. To do this, log into your Drupal environment as an administrator and go to the Themes page (Administer > Site Building > Themes), and click the Save button. This will force Drupal to rescan your theme, picking up any new files (node.tpl and template.php), as well as including the new region being defined from the .info file.

Rebuild the theme registry in Drupal

Add the new region to the node template

Everything you’ve done so far has been to support this final step, which is to actually return the region in the template. Open your theme’s node.tpl.php, and paste this wherever you like (hint: Just below $content):

<?php if($node_bottom): ?>
  <div class="node-bottom">
    <?php print $node_bottom; ?>
  </div>
<?php endif; ?>

Add blocks to your new region

Everything’s done! All you need to do now is add a block or two to this region (Administer > Site Building > Blocks).

Chris Toler