Adding and removing regions from a Drupal template

by John McGeechan

Synopsis

This article discusses how to create and remove regions within a theme.

  • This article assumes some knowledge regarding drupal theming.
  • The reader is creating a new theme or modifying an existing theme and new regions require adding to the template or existing regions require removal
  • This article relates to drupal 5.x, it is unclear at this time if it applies to drupal 6

Specifying regions

The default regions for themes are ...

  • left
  • right
  • content_top
  • content_bottom
  • header
  • footer

These can be overriden by creating a regions function within the template.php file within the themes folder....

function yourthemename_regions() {

return array(

'left' => t('left sidebar'),

'right' => t('right sidebar'),

'content_top' => t('content top'),

'content_bottom' => t('content bottom'),

'header' => t('header'),

'footer' => t('footer'),

);

}

So for instance to remove the left column and to rename the right column to foobar ....

function yourthemename_regions() {

return array(

'foobar' => t('foobar'),

'content_top' => t('content top'),

'content_bottom' => t('content bottom'),

'header' => t('header'),

'footer' => t('footer'),

);

}

Allocating blocks to regions

Once the regions have been created simply allocate the blocks to the regions in the usual way Administer > Site Building > blocks.

It is important to ensure that the theme's page.tpl.php now holds the correct region names. Drupal will assemble all of the content for a region based on knowing which blocks belong to which region, however it is page.tpl.php that specifies whereabouts the regions appear in the page. Ensure that you remove any regions that you don't need from page.tpl.php and include any new ones using the following format ....

<?php if ($foobar): ?>

<div id="foobar">

<?php print $foobar ?>

</div>

<?php endif; ?>

Other Issues

  • Please be aware that drupal uses the variables $sidebar_left and $sidebar_right for regions $left and $right (I am still unclear why)
  • Even if a region is not specified in a template it should still be removed if that region contains no modules/blocks in the eventual page. This is because Drupal will still process a region if it is part of the theme, even if the eventual outcome is that this region will be empty. This is an unneccesary overhead so simply remove it altogether