How to insert a block into a node or template in D

2019-03-07 19:35发布

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?

15条回答
Luminary・发光体
2楼-- · 2019-03-07 20:22

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.

查看更多
The star\"
3楼-- · 2019-03-07 20:22

Improving wrburgess' answer, you can do it in one line...

<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('module_name', 'block_delta'))))); ?>

So for example, I use block number 6...

<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('block', '6'))))); ?>
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-07 20:23

Just tested this in drupal 7 and it works:

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

Good luck!

查看更多
Lonely孤独者°
5楼-- · 2019-03-07 20:25

Have a look how Drupal does it in _block_render_blocks. The result of that function gets passed to drupal_render.

查看更多
淡お忘
6楼-- · 2019-03-07 20:26

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;
  ?>
查看更多
老娘就宠你
7楼-- · 2019-03-07 20:27
 $block = module_invoke('menu_block', 'block_view', '6');
 echo render ($block['content']);

This works for me for printing menu block.

查看更多
登录 后发表回答