How to Add New Region to a Drupal Theme
PHPTemplate engine has 5 regions – left, right, content, top and bottom. This article would tell you how to create new non-standard region for blocks allocation.
Regions are specified areas for any information to be located including Drupal blocks, menu, PHP-code or HTML.
Please leave two standard regions – $content и $footer_message – when you redefine standard blocks with your blocks within ThemeName_regions() function. $content region is necessary for Drupal to show content of a site and $footer_message region is used in control panel to set content of the bottom area of a site.
Do not use hyphens, spaces or other symbols in names of your regions. Letters, digits and underscore only!
Solutions
- Add new region in a theme file
- Add new region to code of a module
- Inline regions
Add new region in a theme file
Drupal 6
- In ‘theme_name.info’ file:
regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer
regions[footer] = Footer
regions[new_region] = New RegionNote
All standard regions (for example, from garland theme) should be defined as well. - Call the variables above in page template file page.tpl.php :
<?php if ($new_region): ?>
<?php print $new_region ?>
<?php endif; ?> - Clear cache on Administer -> Performance ->Clear cache or disable and enable theme again
Drupal 5
theme_regions function allows to set array of regions identifiers (that are used in a theme) and their names.
- Create template.php in theme folder.
- Define the function in template.php:
function theme_name_regions() {
return array(
‘right’ => t(‘right sidebar’),
‘content’ => t(‘content’),
‘header’ => t(‘header’),
‘footer’ => t(‘footer’),
‘new_region’ => t(‘new region’) // <—new region is defined
);
}Note
You should specify theme name in a name of the function. - Add this code to theme file page.tpl.php:
<?php if ($new_region): ?>
<?php print $new_region ?>
<?php endif; ?>
Add new region to code of a module
Usually regions contain data from blocks assigned via Drupal control panel. But sometimes you need to specify a region that will not be available for Drupal blocks. To do so, you should use drupal_set_content() function.
App. algorithm:
- Set value of region variable in the code of your module.
<?php
$output = ‘whatever';
drupal_set_content(‘region1′, $output);
?>Please keep in mind that there is no need to define region variables in theme_name_regions() function.
- Define area of a template where region will be available in _phptemplate_variables() function (this is ‘page’, i.e. general page template in the example below). Add variables of your regions to the general region variables array via drupal_get_content().
<?php
function _phptemplate_variables($hook, $variables) {
// Load region content assigned via drupal_set_content().
if ($hook == ‘page’) {
foreach (array(‘region1′, ‘region2′) as $region) {
$variables[$region] = drupal_get_content($region);
}
}
return $variables;
}
?> - Region variables are now available in page.tpl.php template (as it was defined in the previous step) and we can print them out:<?php print $region1; ?>
Inline regions
You can allocate custom regions as within main template (page.tpl.php) as well within smaller templates like node, page, comment, blog etc.
Drupal6
- Add the following function to template.php file of your theme:
<?php
// instory – name of your region (you can change it)
// “THEMENAME” change with name of your theme.
function THEMENAME_preprocess_node(&$variables){
if(!$variables[‘teaser’]) {
// Load region content assigned via blocks.
// Add instory variable to node variables array
foreach (array(‘instory’) as $region) {
$variables[$region] = theme(‘blocks’, $region);
}
}
}
?> - To make region available, add the following line to THEMENAME.info file of your theme
regions[instory] = instory
- Then you shold print this region in appropriate place of node.tpl.php file
<?php if ($instory): ?> // check blocks in instory region
<?php print $instory ?> // print instory region
<?php endif; ?>
Drupal 5
- Open template.php file for your theme.
- Find _phptemplate_variables() function or crete it if required. In this function, you shold define what region will be available for different node types (passed to template for this node type). Function is called automatically.
Examples:
<?php
function _phptemplate_variables($hook, $variables) {
// Load the node region only if we’re not in a teaser view.
if ($hook == ‘node’ && !$vars[‘teaser’]) {
// Load region content assigned via blocks.
foreach (array(‘inline_for_node’) as $region)
$variables[$region] = theme(‘blocks’, $region);
}
}
return $variables;
}
?>
$hook variable when called contains node type a template is processed for. It is ‘node’ type in our example. Check variable and continue if it contains ‘node’ line.
There is an additional verification which defines is full article text is shown or its preview. In our example, the region should be shown in full text.
In the next two lines we add new variable containing the region to node variables array. Now our region will be available in a form of variable $inline_for_node in node.tpl.php file. We can call it with the help of this:
<?php print $inline_for_node; ?>
Configuration
Change inline_for_node line to names of new regions (separated by comma) to set regions names.
Example: for ‘node’ and ‘comment’ node types:
<?php
function _phptemplate_variables($hook, $variables) {
// Load region content assigned via blocks.
// Load the node region only if we’re not in a teaser view.
if ($hook == ‘node’ && !$vars[‘teaser’]) {
foreach (array(‘node1′, ‘node2′) as $region) {
$variables[$region] = theme(‘blocks’, $region);
}
}
else if ($hook == ‘commment’) {
foreach (array(‘comment1′, ‘comment2′) as $region) {
$variables[$region] = theme(‘blocks’, $region);
}
}
return $variables;
}
?>
I cant get clear