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

后端 未结 15 1099
闹比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:20

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

    0 讨论(0)
  • 2021-01-30 02:21

    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

    0 讨论(0)
  • 2021-01-30 02:24

    Recently I faced the same issue and I came across a nice solution which describes the solution in drupal as drupal's way.

    You can print regions inside any template, but they aren't available out of the box in the node.tpl.php template. To make them available, you'll create a new variable for use in your node.tpl.php template that'll contain all the region content.

    Creating new template variables is done by using a preprocess function. In your theme's template.php file, create a function that looks like this:

    function mytheme_preprocess_node(&$variables) {
      // Get a list of all the regions for this theme
      foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
    
        // Get the content for each region and add it to the $region variable
        if ($blocks = block_get_blocks_by_region($region_key)) {
          $variables['region'][$region_key] = $blocks;
        }
        else {
          $variables['region'][$region_key] = array();
        }
      }
    }
    

    Then, in your theme's node.tpl.php template, you can render any region by doing the following:

    <?php print render($region['sidebar_first']); ?>
    

    Where sidebar_first is the name of the region you want to render.

    Read the complete article here: https://drupal.stackexchange.com/questions/20054/can-regions-be-printed-within-a-node-template

    0 讨论(0)
  • 2021-01-30 02:25

    module_invoke Working fine for render block in the template file, but it's not working multilingual sites.

    0 讨论(0)
  • 2021-01-30 02:27
     $block = module_invoke('menu_block', 'block_view', '6');
     echo render ($block['content']);
    

    This works for me for printing menu block.

    0 讨论(0)
  • 2021-01-30 02:29

    The module_invoke() function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.

    As commented before in other answers, this works as well and also makes use of custom templates:

    $raw_block = block_load('your-module', 'delta');
    $rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
    print $rendered_block;
    

    So, if you have a custom block--your-module--delta.tpl.php template file, it will be used to format the block.

    Source: http://api.drupal.org/api/drupal/includes!module.inc/function/module_invoke/7

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