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

...
regions[BLOCK_REGION_NAME] = Block region name
...

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

/**
* Implements template_preprocess_node().
*/
function YOUR_THEME_preprocess_node(&$vars) {
  // Add 'After node' block region inside node.
  if ($block_region_name = block_get_blocks_by_region('BLOCK_REGION_NAME')) {
    $vars['BLOCK_REGION_NAME'] = $block_region_name;
  }
}

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

<?php if ($BLOCK_REGION_NAME): ?>
  <div class="blablabla">
    <?php print render($BLOCK_REGION_NAME); ?>
  </div>
<?php endif; ?>

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.
$reaction = context_get_plugin('reaction', 'block');
$vars['BLOCK_REGION_NAME'] = $reaction->block_get_blocks_by_region('BLOCK_REGION_NAME');

10 comments for 'Embed Drupal block region into node page'

Rooby's picture

It is worth noting that you will have issues if you are using the context module to assign your blocks to regions.

This is because this drupal core function doesn't know about context blocks.

To get context blocks for a region you can do this:

// Get the context blocks for the sidebar_second region.
$reaction = context_get_plugin('reaction', 'block');
$vars['BLOCK_REGION_NAME'] = $reaction->block_get_blocks_by_region('sidebar_second');

admin's picture

Thanks Rooby for pointing that out!

I've edited my post above and included your snippet to get Context block region to work =)

Craig's picture

What would be a use case for doing this? I'm trying to understand how I would go about using this.

admin's picture

Quite simply, you want a more flexible way of putting blocks into your Drupal website.

For example, in your node full page view, you have comments below it.
But what if you want to place advertisements just after the node content and before the comments? This little snippet will let you do that without hardcoding any blocks.

Craig's picture

Would I first define the region in my .info file but not have the region anywhere in my page.tpl file? Then use the above code to add the region in the node.tpl file?

Or is this dynamically creating a region on the fly?

admin's picture

Oh yes sorry for not making it clear.

You are right, you need to define the new block region in your .info file first.
You don't need to render it in page.tpl.php anymore (depends on you).

Then, yeah add the code above to node.tpl.php =)

Craig's picture

Ah, cool. That makes sense now. thanks for clearing that up!

I can already think of ways I can use this. Thanks again!

Wswizan Zaini's picture

Hello! thank you for this article. It works for me. Now the Social Widget is immediately after the body content and before the comments region. In action here: lizaimilihin.com

Amos Tucker's picture

This uses a View to allow for the browsing of a node to embed and provides a set of template suggestions that allow for the detailed control of node rendering when in embedded mode.

Pierre's picture

The best and the easiest tutorial i found to insert my webform block region into node page.