By default, drupal provides a number of regions to put blocks in and lets you put them pretty much anywhere in the page you want to. But, there are a few exceptions to this rule. What if you want to put a region in a node? What if you want to put a region in between a node and the comments on that node? This may not seem so easy out of the box. Let's take a look at how to put a region in a node.tpl.php file.
Let's walk through a situation where I want to put a custom region between a node and it's comments. I could use this to place something like a block from the Similar by Terms module right under my posting.
I'll start by adding a custom region called node_region using hook_regions() in my themes template.php file.
<?php
function mythemename_regions() {
return array(
'left_sidebar' => t('left sidebar'),
'right_sidebar' => t('right sidebar'),
'header' => t('header'),
'footer_message' => t('footer'),
'content' => t('content'),
'node_region' => t('node region'),
);
}
?>
You'll notice that I defined the same regions that come by default as well as the new node_region. This function defines all the regions, not just additional ones. In this case, I defined the same ones that come by default and just added my additional region.
This now created the node_region so you can assign blocks to in on your blocks admin screen.
Next we need to make the variable $node_region accessible in the node.tpl.php file. To do this we use the function _phptemplate_variables.
<?php
function _phptemplate_variables($hook, $variables) {
if ($hook == 'node') {
$variables['node_region'] = theme('blocks', 'node_region');
}
return $variables;
}
?>
This will made $node_region available in node.tpl.php files. This same concept applies for comments and most of those other non-intuitive places you may want to put a block where it isn't obvious how.
Note: This works for drupal 5. Implementing this is slightly different in drupal 6.
This is useful since you
This is useful since you could target a region to a specific node type and not have to bother with block admin settings. Thanks!
Awesome!
Exactly what I needed! Great tip!
Glad to help
Glad I could help.
Put it on Drupal.org!!!
Hey Matt. I think this should really be moved to the Drupal.org handbooks, if it's not there already.
Added to d.o
Good call. I added it in a slightly re-factored form to drupal.org. You can find it at http://drupal.org/node/208869.
Drupal 6?
Could you possibly do a quick follow up to cover what's different in Drupal 6 for achieving this same effect? I'm trying to get ahead of the game for when it's released and this is a roadblock for a site I'm working on.
In The Next Few Weeks
In the next few weeks I can post about this.
almost too easy in Drupal 6
You just add your region to the .info file of the theme, and voila, the core system populates a variable for you of the same name, and your region will be there for drag-and-dropping blocks to it. Done :)
Still Need To Add It To Node Variables
The .info file lets you add the region. We still need to make the region available via mythemename_preprocess_node or a direct theme call in the node.tpl.php file.
I'll post more on that in a few weeks.
http://www.darrenmothersele.c
http://www.darrenmothersele.com/drupal-blog/node-reference-create-drupal...