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

后端 未结 15 1090
闹比i
闹比i 2021-01-30 01:46

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         


        
相关标签:
15条回答
  • 2021-01-30 02:30

    In my search to include a block in a template, i came across this post.

    As an addition, if you want to include a custom block (that you added through the block interface) you have to use (instead of block_load(); in drupal 7)

    $block = block_get_custom_block($bid);
    $content = $block['body'];
    
    0 讨论(0)
  • 2021-01-30 02:32

    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;
      ?>
    
    0 讨论(0)
  • 2021-01-30 02:32

    There's module called insert_block for those which want to insert block "Drupal way" (not to program anything, just enable the module). Here's how to set it up.

    NOTE: I know this question is about "programmatically inserting a block into a template or node" but Google sends people here even their are looking for non-programmer solution like me.

    0 讨论(0)
提交回复
热议问题