In Drupal 6, it was easy to insert a block into a template with the following code:

    $block = module_invoke('views', 'block', 'view', 'block_name');
    print $block['content'];

However, using the same instructions in Drupal 7 does not seem to work. I have looked around and cannot find what the new method.

Does Drupal 7 have a routine that can allow for pro grammatically inserting a block into a template or node?

CommentFileSizeAuthor
#15 image map with tooltip.JPG42.92 KBjmatuska

Comments

wrburgess’s picture

This appears to be the solution, but it seems a bit clunky and I have no idea about impact on performance:

$block = block_load('views', 'block_name');      
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));        
print $output;
tinny’s picture

what wrburgess wrote works for me.

i added it to a node body field with php filter on.

'views' is the name of the module which created the block
'block_name' is the name of the block which can be found by going to the blocks admin page and hovering over the configure link.

i used it on the calendar module which uses a view to create the block:

$block = block_load('views', 'calendar-calendar_block_1');      
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));        
print $output; 
morbus iff’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

pfrenssen’s picture

This also works and does not call any private functions:

function MODULE_render_block($module, $delta) {
  $block = module_invoke($module, 'block_view', $delta);
  $block['module'] = $module;
  $block['delta'] = $delta;
  $vars = array('elements' => array('#block' => (object) $block, '#children' => render($block['content'])));
  return theme('block', $vars);
}
yskel’s picture

The snippet from wrburgess works for me, but I had to use the syntax:

$block = block_load('views','[view_name]-[block_name]');
kairrow’s picture

This syntax worked for me as well. Thank you

Anandyrh’s picture

I use below code to embed view in to a node, it works perfectly

<?php

$block = block_load('views', 'block_name');      
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));        
print $output; 
?>

Now how do I pass 'CONTEXTUAL FILTERS' to this view?

pfrenssen’s picture

Anandyrh, if you want to embed a view, then you can better use views_embed_view().

If you only need to render the content of a block (excluding the block title and containers), this simple code is sufficient:

  print render(module_invoke('MODULE_NAME', 'block_view', 'BLOCK_DELTA'));
Anandyrh’s picture

Thanks a lot pfrenssen, I'll try it out.

b.ravanbakhsh’s picture

pfrenssen’s picture

@b.ravanbaksh: don't follow the advice in the blog post you mentioned in #12: it bypasses the access control which is provided by views_embed_view(). This is bad practice and a security risk, potentially exposing sensitive data to unauthorized users.

arnoldbird’s picture

This code does not work for custom block module blocks. For example, a block whose delta is 1. This does not work:

<?php
  print render(module_invoke('block', 'block_view', '1'));
?>
jmatuska’s picture

StatusFileSize
new42.92 KB

The revised syntax from #7 works for me also, adding the " [view_name]- " in front of [block_name]. Puts the block into my node very nicely.

However, now i have a real test for you all. I'm trying to add my block INSIDE a wz_tooltip that is launched from an image map. I've made my tooltip sticky so it doesn't close, and I can add php inside the tooltip successfully, but as soon as I try to put this block code inside the tooltip code, it just ignores it and ends up putting the block further down the page AFTER the image map.

Any ideas?
Thanks for any help!
:) jean

i tried to put my code here for the image map and js tooltip, but i couldn't get it to work....

pipep’s picture

#9 did the trick for me printing a block from page.php.tpl in D7

$block = block_load('module_name', 'block_name');      
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));        
print $output; 

Thank you very much.

amitnaik’s picture

#9 working perfect!!! just replace the delta of your block and its done! thanks @Anandyrh

FAAREIA’s picture

Just for the record. #9 is semi-correct but Drupal logs a warning message, since drupal_render need to be "alone".

<?php
$block = block_load('module', 'block-name/delta');
$output = _block_get_renderable_array(_block_render_blocks(array($block)));
print drupal_render($output);
?>
ErikU’s picture

Awesome, thanks FAAREIA! #18 is the one to use as it doesn't cause Drupal to throw a warning message at all!!

charles belov’s picture

Please clarify #18. If cursoring over the configure link reveals that the module is "nodeblock" and the ID is 105487, that is, we are working with a block that can also be reached via /node/105487, so I tried the following code:

<?php
$block = block_load('nodeblock', '105487/delta');
$output = _block_get_renderable_array(_block_render_blocks(array($block)));
print drupal_render($output);
?>

then

<?php
$block = block_load('nodeblock', '105487');
$output = _block_get_renderable_array(_block_render_blocks(array($block)));
print drupal_render($output);
?>

neither of which produces any HTML code.

(Yes, I know I wouldn't normally code a constant. I'm just trying to wrap my mind around what the difference is between a block name and a delta. And if I'm not using the literal "delta", then how do I determine the delta?)

charles belov’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

Opening for follow-up comment.

charles belov’s picture

Found the correct code at http://www.ultechspot.com/drupal/insert-block-node-or-template-drupal-7.

So my code would be:

$legendblock = module_invoke('nodeblock','block_view','105487');
print render($legendblock['content']);

where 'block_view' is a constant and the other two values are variables.

charles belov’s picture

Status: Active » Closed (works as designed)
sobi3ch’s picture

#6 extended version with title/subject

function MODULE_render_block($module, $delta) {
  $block = module_invoke($module, 'block_view', $delta);
  $block['module'] = $module;
  $block['delta'] = $delta;
  
  $block_object = block_load($module, $delta);
  $block['subject'] = $block_object->title;
  $block['region'] = $block_object->region;
  $vars = array('elements' => array('#block' => (object) $block, '#children' => render($block['content'])));
  
  return theme('block', $vars);
}
rahul_sankrit’s picture

Here is sample code to render the views pro-grammatically in Drupal 7 in just one line:

  print views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID', $view_arg);

Where:
VIEWS_MACHINE_NAME: The machine name of the view we wanted to render.
DISPLAY_ID: The display id which, we want to render of that view.
$view_arg: Argument value our view accepts. e.g. I've a view which display taxonomy on the basis of parent taxonomy, so this will be $term->tid.

More Details is here : https://api.drupal.org/api/views/views.module/function/views_embed_view/7.x-3.x

jansohan’s picture

The below code not showing anything for the custom block render in the template file.

print render(module_invoke('block', 'block_view', '1'));