Embed Drupal block region into node page
Snippet to insert or embed Drupal block region into a node template.
First of all, define the block region in your theme's .info file.
YOUR_THEME.info
...<br>regions[BLOCK_REGION_NAME] = Block region name<br>...
In your theme's template.php file, place the following code into template_preprocess_node
. If it does not exist, define it with the code below:
Replace BLOCK_REGION_NAME with your custom block region machine name.
template.php
/**<br> * Implements template_preprocess_node().<br> */<br>function YOUR_THEME_preprocess_node(&$vars) {<br> // Add 'After node' block region inside node.<br> if ($block_region_name = block_get_blocks_by_region('BLOCK_REGION_NAME')) {<br> $vars['BLOCK_REGION_NAME'] = $block_region_name;<br> }<br>}
Next and final step, edit the node template file to embed this block region and place the code snippet below anywhere you want it to display.
node.tpl.php
<span style="color: #000000"><span style="color: #0000BB"><?php </span><span style="color: #007700">if (</span><span style="color: #0000BB">$BLOCK_REGION_NAME</span><span style="color: #007700">): </span><span style="color: #0000BB">?></span></span><br> <div class="blablabla"><br> <span style="color: #000000"><span style="color: #0000BB"><?php </span><span style="color: #007700">print </span><span style="color: #0000BB">render</span><span style="color: #007700">(</span><span style="color: #0000BB">$BLOCK_REGION_NAME</span><span style="color: #007700">); </span><span style="color: #0000BB">?></span></span><br> </div><br><span style="color: #000000"><span style="color: #0000BB"><?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?></span></span>
Edit:
Thanks Rooby for pointing out that Drupal core's block doesn't play nice with Context blocks.
To get Context module blocks:
// Get the context blocks for the sidebar_second region.<br>$reaction = context_get_plugin('reaction', 'block');<br>$vars['BLOCK_REGION_NAME'] = $reaction->block_get_blocks_by_region('BLOCK_REGION_NAME');
Tags: drupaldrupal 7themingthemehow toguideembedblockblock regionnode