We Rock Your Web in the Press!

Drupal – how to display block in node?

Home Forums Tech Troubleshooting Drupal – how to display block in node?

Post a New Topic

This topic contains 50 replies, has 3 voices, and was last updated by  Alex Schenker 1 year, 2 months ago.

  • Author
    Posts
  • #10141

    Is there a way to display a block within a node in Drupal?

  • #10243

    Alex Schenker
    Keymaster

    This is one of the questions we receive most, be it via contact form, comments, or a forum topic: how do you display, or insert, the contents of a Drupal sidebar block within a node, or page? Here’s how.

    Drupal’s Insert Block Module

    First we should point out that you can now install a module that will take care of most of the heavy lifting for you. Insert block will let you insert the contents of a block just about anywhere: a page, template file, etc.

    Insert Block Usage

    After enabling the block and adjusting the input format to allow for the “insert block” filter, to insert a block you use the following syntax:

    • Within a page (in edit mode): [block:name of module=delta of block]
      • For example, you can hover over the “configure” link of a block, which will show you the following URL format (Drupal 6): /admin/build/block/configure/block/1. In this case you would use [block:block=1]
      • A more complex example (Drupal 7): /admin/structure/block/manage/views/cloud_carousel-block_1. In this case you would use [block:views=cloud_carousel-block_1].

    Note that there are disadvantages to using this module. It may interfere with page caching, for example. For a more direct approach, see below.

    How do I place a Drupal block’s contents into a node Manually?

    The easiest and most direct way to place the contents of a block in a page is by using the following PHP snippet:

    Drupal 7

    <?
    $block = module_invoke(‘menu’, ‘block_view’, 26);
    print render($block);
    ?>

    Drupal 6 and Earlier
    <?php
    $block = module_invoke(‘menu’, ‘block’, ‘view’, 26);
    print $block[‘content’];
    ?>

    Placing any Drupal block within a node

    In this particular example, we’re displaying a menu block with ID 26 inside a page. Here’s how you use this code snippet to insert any block (this example uses Drupal 5.x; for other Drupal versions, the process should be similar):

    • Browse to Site Building -> Blocks -> List
    • Browse over the “configure” link next to your block

    This will reveal the following address line (in this example we are using a custom block with ID 26)

    /admin/build/block/configure/block/26

    the last two variables are the ones we’re interested in: /block/26
    This works for any content type, and any block
    If we wanted to place this custom block into a page, we would simply substitute these values into our PHP code snippet (which we place in a node (page or other content type)) as follows:

    Drupal 7
    <?php
    $block = module_invoke(‘block’, ‘block_view’, 26);
    print render($block);
    ?>

    Drupal 6 and Earlier
    <?php
    $block = module_invoke(‘block’, ‘block’, ‘view’, 26);
    print $block[‘content’];
    ?>

    Notice that the middle two terms (‘block’, ‘view’) always stay the same. You’re simply updating the first and last term.

    Voila :)

    What if I want to display a node in a block?

    For the reverse method, displaying a node (such as a page) in a block, we recommend you use the node as block module.

    What if I Simply Need to Print a Block in a Theme File?

    If you don’t need your block to appear within your node content, you can simply have it printed within your theme file (page.tpl.php for example), as follows:

    <?php
    print $before_content;
    ?>

    First you’ll want to add the corresponding region (before_content) to your theme’s info file (theme-name.info):

    regions[before_content] = Before Content

    You may need to visit your theme listing page to have your theme cache refreshed and the region applied to your theme.

  • #10158

    Guest
    Participant

    I would like to place a webform form which is a node into a page/node. Is the code very similar to placing a block in a node?

    Thanks
    Rory

    • #10160

      Alex Schenker
      Keymaster

      Hi Rory,

      I’m pretty sure it’s possible, but you might be better off simply inserting what you had in the other node in the description field of the webform – it will take any input format, so you should be able to put just about anything in there.

  • #10163

    Guest
    Participant

    This is so simple, isn’t there a module for this? I mean come on …

    • #10164

      Alex Schenker
      Keymaster

      It is simple. If you want this installed as a module, why don’t you learn how to create the module? Most Drupal developers are bogged down getting core functionality improved. This could be a good starting point for you to contribute to Drupal.

      • #10165

        Guest
        Participant

        I like what I’ve read about Drupal so far and plan on experimenting around with it. In fact, I think I will give this a shot. Cool. Depends what I decide to do next with my email marketing business and if that’s the direction I even want to go with it.

  • #10161

    Guest
    Participant

    Thanks for the code.

    The first example does not work, because the word “menu” is there, when it should be “block”.

    • #10162

      Alex Schenker
      Keymaster

      I probably should have made that more clear. The first example is for inserting a menu into a node, the second for inserting a block.

  • #10167

    Guest
    Participant

    non-php version of this functionality

    The reptag module provides this sort of functionality through a filter. Use markdown code like the following:

    “{BLOCK:1}” => “user-defined block 1″, “{BLOCK:Contact Details}” => “user-defined block with the given title”, “{BLOCK:menu:2}” => “block 2 supplied by the named module”, “{BLOCK:image:Random image}” => “named block from the given module”

    Pretty handy if you like it.

  • #10166

    Guest
    Participant

    Some of the blocks do not have a number on them. I made a view, but it didn’t give it a number… I’m confused.

  • #10168

    Guest
    Participant

    I believe that the easiest way to do that is to use a node-specific page template. And if you want to display multiple nodes you can create templates for each of them.

  • #10169

    Guest
    Participant

    When inserting a Drupal block within another Drupal block or node – does the input format get inherited (as with an iframe), or does the input format of the parent node override that of the inserted node or block?

    • #10170

      Alex Schenker
      Keymaster

      Good question – the input format of the inserted node will apply to the inserted portion, while the input format of the parent node will apply to the rest of the node. In other words, the insert will be treated as an iframe.

      • #10171

        Guest
        Participant

        Most Drupal developers are bogged down getting core functionality improved. This could be a good starting point for you to contribute to Drupal.

  • #10173

    Guest
    Participant

    If you want to display multiple nodes you can create templates for each of them.

    jollie

  • #10174

    Guest
    Participant

    Thanks for the script! Indeed very helpful.

  • #10175

    Guest
    Participant

    Hey,

    Good Post. But instead of using the block id 26 can’t we use the name of the block or some other parameter?

    • #10176

      Alex Schenker
      Keymaster

      Simply use the ID corresponding to the block you want to insert. There may be a way to do this by block name, but we haven’t come across one yet.

  • #10177

    Guest
    Participant

    The module Display Suite (DS) can do this for you.

  • #10178

    Guest
    Participant

    Thanks for the snippet. I was trying to remember how to do that. Drupal always gives me a headache it seems.

  • #10180

    Guest
    Participant

    I want to include my block from similar entries module, but it doesn’t simply have a number, but “configure/similar/0″. Any idea on how to include this block? Great post, thanks! :-)

  • #10182

    Guest
    Participant

    My code is like the visitor that just posted:

    configure/views/Prices-block_1

    I tried using block id 1, but that did not work. I will try to figure out what its real id is… any good way to do this?

  • #10183

    Guest
    Participant

    This works great but is there a way to pass in arguments?

    I have created a view that requires an argument to be passed in. Can you pass in arguments using your code? Any help is greatly appreciated!

    $block = module_invoke(‘views’, ‘block’, ‘view’, 26);
    print $block[‘content’];
    ?>

  • #10185

    Guest
    Participant

    Mighty thanks for the snippet. Worked like a charm :-)

  • #10186

    Guest
    Participant

    You know, the more I got into Drupal the more I disliked it. Now I want nothing to do with it. I hate it actually.

  • #10187

    Guest
    Participant

    I need to show one certain block in one menu and other block in another menu. Please any body help me.

  • #10189

    Guest
    Participant

    This is why I prefer Drupal over other CMS systems such as Joomla, WordPress, etc. – the flexibility. Not only in terms of coding and theming, but in being able to theme and adapt customizations of actual modules and core code, without disrupting the core code. In other words, being able to apply your own theme to things to get things done. Such as is the case with the simple ability of displaying a block within a node. Cheers!

  • #10191

    Guest
    Participant

    More info on embedding block and getting the whole thing rendered: http://drupal.org/node/164799

  • #10193

    Guest
    Participant

    In Drupal the 2 middle arguments that never changed: block and view “merge” into block_view.

    Drupal 6: $block = module_invoke(‘block’, ‘block’, ‘view’, ‘block_name’);

    Drupal 7: $block = module_invoke(‘block’, ‘block_view’, ‘block_name’);

    William B

    • #10194

      Alex Schenker
      Keymaster

      Thanks for the heads up William! We’ve updated the article to reflect this change.

  • #10195

    Guest
    Participant

    Thanks for making my life easier :-)

  • #10196

    Guest
    Participant

    Hi there – I have a block for similar entries. And I want to show similar entries as part of the node. But NOT in the teaser. If I add it to the node it shows up in the teasers on my front page. How do I remove the similar entries from the front page?

    • #10197

      Alex Schenker
      Keymaster

      You can use this code snippet in your theme’s node.tpl.php file:

      < ?php if ($page == 0): ?>
      $block = module_invoke(‘similar’, ‘block’, ‘view’, 0);
      print $block[‘content’];
      < ?php endif; ?>

      This snippet prints the contents of your “similar” block, but only if a page, not the front page, is detected.

      • #10198

        Guest
        Participant

        It’s better to put that in the template.php file of your Drupal theme folder.

  • #10199

    Guest
    Participant

    By default these blocks display the node title as the block title and the node teaser as the body. This can be themed by overriding the theme(‘nodeasblock’, $node) function.

    I kind of wish Drupal’s node/ block system was modular in a way that would remove the distinguishing characteristics between the two, make the both the same, but allow a “tag” or “category” to distinguish them.

  • #10188

    Guest
    Participant

    Here’s the updated code for embedding a block in node:

    < ?php
    $block = module_invoke('views', 'block', 'view', 'explore_menu-block_1');
    print $block['content'];
    ?>

  • #10200

    Guest
    Participant

    Doesn’t work for me using D7.

    I have clean url’s on, so finding the block id doesn’t work like mentioned in the tutorial. Maybe I can find a newer tutorial by further searching.

  • #10202

    Guest
    Participant

    How do we call a block inside the node, in Drupal 5? Please note that I need this in Drupal 5 (not 6 or 7+).

    • #10203

      Alex Schenker
      Keymaster

      The code for Drupal 6 above works in Drupal 5 and earlier as well.

  • #10204

    Guest
    Participant

    Where to place the code in content-type.tpl.php file?

    Can you please guide me where to place this for a content type having a separate .tpl file.

  • #10206

    Guest
    Participant

    A more complete answer uses a helper from Drupal 7:

    < ?php
    #put in template.php
    function block_render($module, $block_id) {
    $block = block_load($module, $block_id);
    $block_content = _block_render_blocks(array($block));
    $build = _block_get_renderable_array($block_content);
    $block_rendered = drupal_render($build);
    print $block_rendered;
    }
    ?>

    < ?php
    #put in whatever.tpl.php
    block_render('search', 'form')
    ?>

    • #10211

      Guest
      Participant

      You’re right, but may I suggest one adjustment (applies to Drupal 7 only), so everybody can finally get the WHY and HOW: (I get this question way too much on IRC, so I’m going to share this bit!)

      block_id ? People, that’s the DELTA! Not the ID as in table entry id. See http://api.drupal.org/api/drupal/modules!block!block.module/function/block_load/7

      So the function would look like this:

      #put in template.php
      function block_render($module, $block_delta) {
      $block = block_load($module, $block_delta);
      $block_content = _block_render_blocks(array($block));
      $build = _block_get_renderable_array($block_content);
      $block_rendered = drupal_render($build);
      return $block_rendered;
      }
      ?>

      And return the data, don’t Print it inside the function, people might want to do a little bit more to the data once returned.

      #put in whatever.tpl.php
      print block_render(‘module’, ‘block_delta’);
      ?>

      Working example would be:

      print block_render(‘node’, ‘recent’);
      ?>

      or user, login
      or search, form
      or etc etc etc.

      Check your database block table to get the module and delta if you don’t know them.

  • #10207

    Guest
    Participant

    Totally confused when it comes to a custom block I have made with views – how do I get it to display a block created in views that has no ID?

  • #10209

    Guest
    Participant

    I have to say the simple fact that Drupal gives you the flexibility of installing blocks in nodes, or nodes in blocks, etc. makes it so much more powerful than competing platforms such as Joomla, PHP dot Nuke, etc.

  • #10210

    Guest
    Participant

    Bad idea to render a block like that in Drupal 7! What about hook_block_view_alter + family? Shall we just ignore this slightly important hook?

    These are some internal functions I use:

    $block = block_load($module, $delta);
    $key = $module . “_” . $delta;
    $rendered = _block_render_blocks(array($key => $block));

  • #10212

    Guest
    Participant

    Here’s a D7 module for inserting blocks in nodes

    I just added a module to drupal.org; you can give it a try for this sort of thing:

    https://drupal.org/project/region_token

You must be logged in to reply to this topic.