Last updated August 26, 2009. Created by ugerhard on January 11, 2008.
Edited by bekasu, mfer. Log in to edit this page.
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 you want to put a custom region between a node and it's comments. YOu could use this to place something like a block from the Similar by Terms module right under my posting.
Start by adding a custom region called node_region using hook_regions() in your 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 the same regions that come by default are defined here as well as the new node_region. This function defines all the regions, not just additional ones. In this case, you 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 you need to make the variable $node_region accessible in the node.tpl.php file. To do this 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.
Comments
Drupal 6 way
The way you would do the same in Drupal 6 is to modify your theme.info file with regions you want to use:
regions[header] = Headerregions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[footer] = Footer
regions[node_region] = Node region
Next you have to implement a preprocess function in your template.php file. For example:
function phptemplate_preprocess(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
This will enable you to use $node_region in your node.tpl.php file.
Drupal 6 preprocess_node
You can use the preprocess_node function specifically as well and you have to if using a subtheme like for Zen.
function subtheme_preprocess_node(&$variables) {$variables['node_region'] = theme('blocks', 'node_region');
}
in Zen $vars instead of $variables
for me in Zen $vars worked instead of $variables
phptemplate_preprocess_node
I'm pretty sure this:
function phptemplate_preprocess(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
needs to be:
function phptemplate_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
or
function yourthemename_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
Last function worked for me
Thank You very much Jacine!
Jacine: your example worked
Jacine: your version worked swimmingly for D6. Thanks!
kind of
hey -
i setup the new region and it appears and everything, but it still keeps being placed BELOW the comments.
i can go into the node.tpl.php and hand code some HTML in there and it shows up fine where its supposed to.
i'm on drupal 6.x with the barron theme.
this is driving me nuts.
austin, texas - beyoatch.
I also used Jacine's approach
I also used Jacine's approach and works well.
I used in the template.php file:
function yourthemename_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
and then I used $node_region in node.tpl.php file just after the $content printed like that:
<div class="content">
<?php echo $content; ?>
</div>
<div class="node_region">
<?php echo $node_region; ?>
</div>
}
Now blocks appear between content and comments as intended.
Hope it helps...
Sinan
I don't get this to work. My
I don't get this to work. My problems are exactly the same like the ones of nathanmcginty.
I added this to my template.php:
<?phpfunction mw_preprocess_node(&$variables, $hook) {
$variables['below_content'] = theme('blocks', 'below_content');
}
?>
"mw" is my template and "below_content" is the region. Shouldn't it now be possible to print this in my node.tpl.php?
<?phpprint $below_content
?>
If I write this in my node.tpl.php nothing happens.
Please help,
Nico
theme name?
Are you sure your theme name goes as "mw"? maybe it is something about that... Just a suggestion...
Sinan
If your using Zen the
If your using Zen the preprocess function are commented out. Can't believe I spent 15mins trying to get this to work before I realised.
template.php error
I'm getting this error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING in mytheme/template.phpBlog Marco | Best Designed Drupal Websites
Thanks for the expanded
Thanks for the expanded explanation. Really helped.
Works Wonderfully (D6)
I'm not adding anything, just summarizing all the steps given above (and on other posts) into one complete entry for Drupal 6.
Notes:
Step one, add this (without the PHP tags) to your theme.info file. Verify default regions
haven't changed.
<?phpregions[header] = Header
regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[footer] = Footer
regions[node_region] = Node region
?>
Step two, add this (without the PHP tags) to your template.php file. Change "yourthemename" to your theme name.
<?php/**
* Embed a Region in a node.tpl.php file
* drupal.org/node/208869
*/
function yourthemename_preprocess_node(&$variables, $hook) {
$variables['node_region'] = theme('blocks', 'node_region');
}
?>
Step three, add this (with the PHP tags) to your node.tpl.php file.
<div class="content"><?php print $content?></div><?php if ($node_region) { ?><div class="node_region"><?php print $node_region?></div><?php }; ?>
<?php if ($links) { ?><div class="links"><?php print $links?></div><?php }; ?>
Step ...
Hope that makes it easier for the next person,
Michael
Thanks go to:
wesku: Modify your theme.info file
Jacine: Correct phptemplate_preprocess_node code
etcetera9: Placement of $node_region in node.tpl.php
Thank you sooooo much for this.
Just wanted to write a quick thanks for this - saved my ars!
Thanks a lot I have done this
Thanks a lot
I have done this and it is working, but I can't manage to display any fields in the custom node regions.
I'm pretty sure that I'm missing something. Could anyone point me the right way to do this?
Be Free, or Die Trying.
Perfect documentation.
Perfect documentation. :)
Thanks.
-----------------------------
Subir Ghosh
Newswatch
www.newswatch.in
What should we do if we want
What should we do if we want more than one such region?
I tried this:
function yourthemename_preprocess_node(&$variables, $hook) {$variables['firstregion, secondregion'] = theme('blocks', 'firstregion,secondregion');
Didn't work.
-----------------------------
Subir Ghosh
Newswatch
www.newswatch.in
You need to separate lines
You need to separate lines
$variables['firstregion'] = theme('blocks', 'firstregion');$variables['secondregion'] = theme('blocks', 'secondregion');
Thanks a lot, Steve. It,
Thanks a lot, Steve.
It, needless to say, works :)
Cheers.
-----------------------------
Subir Ghosh
Newswatch
www.newswatch.in
Sorting Blocks in node-Region
Hi I have implemented regions in nodes (D6.17), all works fine but the order of the blocks displayed in one region is inverse to what they are in the build/block/list page. Does anyone konw how to fix this ?
I use this to enable the regions in my template.php
// enable regions in node.tpl.php
function netmx_preprocess_node(&$variables, $hook) {
$variables['teaser'] = theme('blocks', 'teaser');
}
Thanx
it's not that simple. I
it's not that simple. I followed it simply and all I get is
function madaxestheme_preprocess_node(&$variables, $hook) {
$variables['node_region'] = theme('blocks', 'node_region');
}
printed at the top of all my pages.
There is no problem in the world that can't be solved with cake and ice cream
Hi, I think you have to
Hi,
I think you have to select php-code als input format.
greetings,
Martijn
Drupal 7 Solution
Could someone please post a Drupal 7 solution, or provide a link to an existing one? Thanks!
Blue Presley
http://gastonia.com