How to put a block inside an article in Drupal
Published on Saturday, Dec 7, 2013
As a Joomla developer I usually put a module inside an article using loadposition code. The code is pretty simple: {loadposition position_name}, but I can’t find this functionality in Drupal 7, I don’t know how to put a block inside an article in Drupal. So I ended adding this code to the page.tpl.php in my template.
$node = menu_get_object();
$nid = $node->nid;
$body = $page['content']['system_main']['nodes'][$nid]['body'];
$content = $body['#object']->body['und'][0]['value'];
$argument_keys=null;
if (preg_match_all("/{loadposition ([\w-]+)}/", $content, $argument_keys)) {
$region = $argument_keys[1][0];
$region_name = block_get_blocks_by_region($region);
$added_string = render($region_name);
$newcontent = str_replace($argument_keys[0],$added_string,$content);
}
if($newcontent==NULL)
print render($page['content']);
else
print $newcontent;
As you can see from this code, it will search for this pattern: {loadposition position_name} from the content of article or basic page, and get the value of position_name. Then it will load the block by region name using function block_get_blocks_by_region and replace this string: {loadposition position_name} with the block content.
Hope this help someone