Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 529
Randy Burgess Avatar asked Jan 19 '11 03:01

Randy Burgess


People also ask

How do I place a block in Drupal 7?

Each administrator-defined block consists of a title, a description, and a body which can be as long as you wish; the Drupal engine will render the content of the block. Enable, throttle, place, and configure blocks at Administer >> Structure >> Blocks. Add a block at Administer >> Structure >> Blocks >> Add.

What is the template file used for the blocks in Drupal?

php file. Really useful for theming, particularly the block description one. The available templates for any block will be: block--REGION.


2 Answers

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

like image 184
kloewer Avatar answered Sep 29 '22 11:09

kloewer


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.

like image 38
Randy Burgess Avatar answered Sep 29 '22 13:09

Randy Burgess