Friday

Drupal 7: How to get rendered blocks in content region


Some modules are added by default to content (as rating and share modules), but others are not allowed to be rendered or added whiten contents or articles.

As an example, you may need to add "Related or Relevant" articles after the article,in this case this topic is what you need.


1.open file: modules/block/block.module

and add the following code:
<?php/**
* Get a block suitable for rendering.
* Note: the block does not have to be enabled in $region.
* @see includes/block.module/_block_render_blocks()
* @see includes/block.module/_block_get_renderable_array()
*/
function siteoverride_embed_block($module, $delta, $region='content') {
 
// Create block stub.
 
$block = new stdClass();
 
$block->module = $module;
 
$block->delta = $delta;
 
$block->region = $region;
 
// Load title.
 
global $theme_key;
 
drupal_theme_initialize();
 
$block1 = db_query("SELECT title FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme",
                    array(
':module' => $module, ':delta' => $delta, ':theme' => $theme_key))->fetchObject();
  if (
is_object($block1) && isset($block1->title)) {
   
$block->title = $block1->title;
  }
 
// Render the content and subject for the block.
 
$blocks = _block_render_blocks(array($block));
 
// Get an array of blocks suitable for drupal_render().
 
$array = _block_get_renderable_array($blocks);
  return
$array;
}
?>


2. open file node.tpl.php and add
<?php
print render(siteoverride_embed_block('block', '0')); ?>

before comments "  <?php print render($content['comments']); ?>" or you can update the code as needed.

 where block: is the name of the block
and
0: is the delta


Note:don't assign region to this block.



for more info: source