How to insert a block into a node or template in Drupal 7?

85,567

Solution 1

With wrburgess's answer you may get an error if your server is using a newer version of PHP.

Strict warning: Only variables should be passed by reference in include()...

This is what I did to not cause/get rid of the error.

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

Solution 2

D7:

<?php
  $block = module_invoke('module_name', 'block_view', 'block_delta');
  print render($block['content']);
?>

'module_name' = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.

'block_delta' = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:

Drupal 7: admin/structure/block/manage/webform/client-block-11/configure

In this example, 'webform' is the module's name, 'client-block-11' is the block's delta.

Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.

More information: http://drupal.org/node/26502

Solution 3

This appears to be the solution for inserting blocks into templates for Drupal 7, 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;

If anyone has a better procedure, please do add.

Solution 4

This work for me:

98 is the id of the block

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

Solution 5

Just tested this in drupal 7 and it works:

$bloqueServicios = module_invoke('views', 'block_view', 'servicios-blo_home');
print render($bloqueServicios);

Good luck!

Share:
85,567
Randy Burgess
Author by

Randy Burgess

I have 22+ years experience as a small business technology leader and hands-on application developer. Website and Mobile App development JavaScript, Typescript, React, React Native, Vue Ruby, Ruby on Rails, Node, Go TDD, Continuous Integration REST API Development, AWS, Heroku, Firebase PostgreSQL, MySQL, SQL Server, Firestore, MongoDB HTML, CSS, Sass

Updated on June 15, 2020

Comments

  • Randy Burgess
    Randy Burgess almost 4 years

    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 the new method.

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

  • jackocnr
    jackocnr almost 12 years
    This is the solution I settled on - as it is the only way I have found of including the block title and contextual links. Thanks.
  • bcosynot
    bcosynot over 11 years
    This did not work for me. I had to use print $block['content']; in Drupal 7.
  • Kevin Siji
    Kevin Siji almost 11 years
    This did not worked for me in the node. I had to use print render($block); for views block and print render($block['content']); for a custom block in Drupal 7.
  • Segfault
    Segfault almost 11 years
    This method will theme the block content with the correct template file. Kloewer's answer will get you the body of the block unthemed.
  • PatrickS
    PatrickS over 10 years
    since php 5.4 , you can only call drupal_render on a variable... meaning that you should get your $output up to _block_get_renderable_array(), then print drupal_render($output)
  • Randy Burgess
    Randy Burgess about 10 years
    Thanks for adding this. I got bitten by the issue two days ago and wasn't sure of the cause.
  • Shaun Dychko
    Shaun Dychko almost 10 years
    This is what I needed to get #attached js included, using render() in a .tpl.php file.
  • Augusto
    Augusto about 9 years
    you will need this when rendering a block you created at admin/structure/block/manage/block/98/configure
  • Stphane
    Stphane over 8 years
    Can anybody tell me what is the difference between drupal_render() and render(), and where should I use each function ? Thank you !
  • canintex
    canintex over 8 years
  • ahimsauzi
    ahimsauzi over 4 years
    Works great, thanks @canintex! I recommend adding a condition to make sure $blockObject exists prior to printing or setting any variables.