Feb 27 2013
Last update:

Drupal - how to display block in node?

Filed under: Content Management Systems » Drupal,
 

Learn Drupal quickly with Drupal videos

If you want to learn all about Drupal by watching easy to understand videos, you won't have to search for individual articles such as this one, which addresses how to insert a Drupal block's contents into a node. Tap into the power of Drupal, slash your production time and create better Drupal websites. These videos include the process of building a fully featured sample Drupal site from scratch. Offered by our partner and Drupal Association Organization Member, these videos are available for a limited time. Check out these exclusive Drupal Videos now.

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

<?php
$block = module_invoke('menu', 'block_view', 26);

print render($block);
?>

Need to Organize? Visit Shoeboxed.com (Affiliate)

 

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 Smiling

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.

Learn Drupal quickly with Drupal videos

If you want to learn all about Drupal by watching easy to understand videos, you won't have to search for individual articles such as this one, which addresses how to insert a Drupal block's contents into a node. Tap into the power of Drupal, slash your production time and create better Drupal websites. These videos include the process of building a fully featured sample Drupal site from scratch. Offered by our partner and Drupal Association Organization Member, these videos are available for a limited time. Check out these exclusive Drupal Videos now.

Your rating: None Average: 2.4 (105 votes)
Your rating: None Average: 2.4 (105 votes)
Email this page PDF of this page Print this page
Leave a comment
(71)



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_tokens

Drupal Compared to WordPress

Another thought I had relates to why Drupal is important in the first place. Sure I understand that many people swear by it. Many web site owners and developers say they would not even think of creating their sites with anything else. But does all of that really make it important? Is it any better than something like WordPress? After all, a web owner could just as easily get very good at using HTML and make some fine looking sites. Or they could learn Flash or another popular language which enjoys a lot of media attention and software support.

As I was researching these questions, I did find some interesting answers which speak to the importance of using Drupal as a web tool. For example, one of the main benefits is that it is open source software. So any other developer is free to add something to the source code. This gives the program the ability to always be adapting and growing. You really never know what you might be able to accomplish with it just around the corner.

This same characteristic also makes it very lithe. This also means that it can be used and adapted to sites of all different sizes, functions and goals. Heck, if there was something that you wanted it to do, you might even be able to convince someone else to add that function to the program (assuming you were not able to add it in yourself).

Drupal is capable of true multiple language support. This is true for any type of website, whether we are talking about a blog, personal site or even a large ecommerce merchant web space.

This system is also quick and powerful. The menus are organized in a very logical and balanced manner. This also makes it very easy to find and use all of its different features and services. So whether you are interested in advanced searching techniques, RSS feed integration, template customization or anything in between, you will be able to do it here. If you do not know what those terms mean, do not worry about it. There are plenty of basic features that can help even the first time web developer.

Drupal also allows support for a number of different databases. These include MtSQL, PostgreSQL, MariaDB and SQLite. It also includes support for JQuery and JavaScript.

I guess what I am trying to say is that in the course of my investigation I became convinced that this is an important tool Drupal can certainly do quite a bit and more features will continue to be added. If you are interested in a system which gives you a lot of power and functionality, without all of the normal headaches associated with programming, then this just might be for you.

Drupal is important. I am also convinced that the author has the right idea by suggesting videos as an excellent way to learn and develop yourself as a drupal web site programmer.

Learning about Drupal

One of the best things about the internet is that there is just so much information available to anyone who is interested. Really. You can find out almost anything you want to know just by doing a few searches and investigating things. This article really brought that very point home for me. After looking at the title I realized that I do not know very much about Drupal. Since I do have my own websites and know a thing or two about internet marketing I have heard of Drupal and know that it is a content management system. However, I have never used it and really did not plan on ever learning it. For my purposes, building WordPress and Blogger sites was enough.

On the other hand, I can also see how learning such a system might help me to become a better all around web designer and programmer. If I had decided to learn a new system such as Drupal, what would I do in order to gain the knowledge and experience required to actually start using it?

I suppose that I could try to find some classes that would teach this. However, I imagine it is such a highly specialized skill that it might be a part of a program or class, but not likely a class in and of itself. Personally, I would not enjoy sitting through either a bunch of stuff that I already knew or was not interested in just to get to the one nugget of information I came for. Also, I would not be too thrilled about having to take my personal time to drive out to the local community college or technical center and sit for 3 hours a night.

Then I thought what about the internet. I could certainly find everything I ever wanted to know online. So I took a quick look around. Certainly there are all kinds of articles and trainings about how to use Drupal. Some is aimed at beginners, some at moderate level users and more at advanced Drupal users. In short, there is something which will help no matter what your skill level.

I also understand that many people do not like to read. I have no problem with reading myself. Actually I learn best from reading. But I will readily admit that not everyone shares this trait. So, videos could be helpful for many people. I took a look and sure enough there are tons of videos about Drupal and all types of programming. Once again, it is more information than any one person could ever possibly hope to sort and sift through. You could very likely create your own self tailored course or video program. The real beauty of this is that it is entirely done at your own pace. If there is something that you do not quite understand, go back to that part. Pause the video and try playing around with that topic in a Drupal simulator or the real thing.
 

Drupal 7 block contents - one more adjustment suggestion

'Submitted by a web rocker on September 23, 2011 - 3:03pm.'

Is Right, but may i suggest 1 adjustment (applies to Drupal 7 only), so everybody can finally get the WHY and HOW: (i get this question way to much on IRC, 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:

<?php
#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.

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

Working example would be:

<?php

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.

Bad practice

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));

Displaying a Drupal block in a Drupal node

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.

Help please?!

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?

Insert block or insert view modules

Have you tried the insert block or insert view modules?

Updated code

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')
?>

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

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

Insert block module

Try the insert block module (Drupal 6.x and 7.x).

Block calling in node Drupal5

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+).

Use Drupal 6 code

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

Drupal rocks!

Drupal kicks butt. By far the best CMS out there IMO. Nuff said.

Thank you very much

U r the best ....ur code is superb ...u have saved lot of my time...thank you very much....cheeeeeeeeeersssssssssss

If u have clean url - try

If u have clean url - try this http://drupal.org/node/1034524#comment-4100718

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.

Block

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.

Remove it from teasers

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?

Similar entries on node only

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.

Place in your template file

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

Hi

Thanks for making my life easier Smiling

This has changed in Drupal 7

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

Thanks William

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

More info

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

How do I place a block into a node

How do I place a block's content into a node in Drupal 7?

Ok here's the deal I got a

Ok here's the deal.

I got a LAN setup at home with the internet coming to the PC assigned an IP 192.168.0.1 (my Internet server).

Now, using Internet Connection Sharing, I have shared the internet access to 2 other PCs who get an IP address dynamically (and are always OTHER than 192.168.0.1).

The thing is, I need to block access to a particular website only on 192.168.0.1 (the internet server for my LAN) - But need to have access on that site on the other machines.

I added an entry to my hosts file on the internet server 192.168.0.1 as following:

127.0.0.1 www.orkut.com

Now, when I try accessing this website via my laptop (which is NOT 192.168.0.1), it says page can not be found -- So the hosts file is functioning as expected. I need to know if there's any way I could block that site only on a particular NODE on the network without having to purchase any paid software.

Drupal over other CMS

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!

Good stuff!

Good stuff!

Updated Code

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'];
?>

How to show certain block in one menu

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

You know, the more I got into

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.

Mighty thanks for the snippet

Mighty thanks for the snippet. Worked like a charm Smiling

Drupal Open Source

Drupal is a powerful open source web development platform. This is easier and surely gives comfort to Internet users. Thanks for sharing. Posts like this offer a great benefit to us users.

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! <?php
$block = module_invoke('views', 'block', 'view', 26);
print $block['content'];
?>

I tried

Thanks..

My code is like the visitor

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?

Similar Entries Module Block

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! Smiling

Try 0

Did you try 0 as the number?

My Drupal impression

I have been building my websites using Wordpress and other platforms and only recently came across Drupal. First impressions are that is has a clean presentation and is well supported with great tutorials.

Thanks

In the evening I usually save all interesting for me articles and read them either in the morning on my way to work or during the dinner break. When I fail to find free time though, I set articles in pdf format and download them.

Thanks for the snippet

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

Display suite module

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

Raghu

Hey,

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

Use any id

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.

node block

Thank you for the information on how to display block in node. Sharing these codes are really gonna be very beneficial.

drupal

Thanks for you the information on how to block drupal using drupal blocking mode.

Is this code same as in blocking a node?

Thanks for sharing this code. But one question though. Is this the same as when you place a block in a node?

Thanks again.

Globat Reviews Guy

Block in node

Yes, that's precisely what it is.

Thanks for the script!

Thanks for the script! Indeed very helpful.

Cool

Thanks for the codes. U really did a good job.

DISPLAY

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

jollie

Drupal node insert - input format?

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?

Inserted content treated as iframe

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.

Core Drupal developers bogged down

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

Drupal

Thanks for the great Drupal tips, will come in very handy!

Good Drupal workaround

Makes everything looks easy with the workaround that you've shown us.

I will keep this in mind

Interesting, when I'm ready to use Drupal and experiment around with it, 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.

Node-specific page template

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.

Some of the blocks

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...

non-php version of this functionality

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.

Number on drupal blocks in view?

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...

This is so simple, isnt there a module for this ...

i mean come on ...

Making a Drupal module

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.

Drupal newbie

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.

Thanks for the code.

Thanks for the code.

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

Inserting Drupal Block vs. Menu

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.

Placing a webform into another node - Possible?

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

Appreciated

Is the code very similar to placing a block in a node?

Webform description

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.

buenisimo!!!!!!!!!!!!!!!!!!

buenisimo!!!!!!!!!!!!!!!!!!
gracias

The content of this field is kept private and will not be shown publicly.
 

We use this trick question to try and prevent automated spam comments.