Add a block within a node.tpl.php file - Drupal 7
01 Aug 2011
To insert a block directly within any node.tpl.php file, or indeed within any page.tpl.php file, you can use module_invoke.
To find the right variables to pass go to the block page within Drupal admin and hover over the 'configure' links for each block. The path tells you the module and block id that make up the first and third variables. The second one should always be 'block_view' if you want to view the block (and we do).
Example: inserting a webform block
To insert a webform block directly into a node tpl, first make sure you've enabled a block from the advanced settings on the webform itself.
Then visit 'Structure' > 'Blocks' and look at the configuration link for the webform block in question.
If the configure link is admin/structure/block/manage/webform/client-block-44/configure then the code to place it will be:
$block = module_invoke('webform', 'block_view', 'client-block-44'); print render($block);
Comments
15 Apr 2012
Jeff Shamley (not verified)
Thanks for sharing...I just used it in one of my tpl files. Placed a menu block into the content area of a certain content type. Worked perfect.
23 May 2012
Harm Jan (not verified)
I needed to print render($block['content']) to get it working properly, but thanks!
25 May 2012
Jack (not verified)
Thanks for this (and to Harm Jan - I needed to add that too).
23 Dec 2012
cipto-junaedy.net (not verified)
I am Cipto Junaedy,
Hey great website! Does running a blog like this take a lot of work?
I have absolutely no expertise in computer programming but I
had been hoping to start my own blog soon. Anyhow, if you have any
suggestions or tips for new blog owners please share.
I know this is off topic but I just wanted to
ask. Thank you!
25 Dec 2012
cesar (not verified)
Doesn't seem to work fine with my Webform.
I tried the above code and the block prints well in the node but when I'm hitting the submit button, it will not send any email. Webform doesn't even add it to the submissions list. When I use the Webform block the normal way, it just works as normal.
03 Jan 2013
Virendra (not verified)
Its Really good and helped me...THANKS
11 Feb 2013
trend busana mu... (not verified)
Google Secret! Don’t pass this around please….
05 Apr 2013
Demaemiain (not verified)
altefalcope <a href="http://vikawieier.com">xaikalitag</a> Nupestemype http://uillumaror.com - iziananatt Gymnsypeshy http://gusannghor.com Icercefaree
12 Apr 2013
Demaemiain (not verified)
altefalcope <a href="http://vikawieier.com">xaikalitag</a> Nupestemype http://uillumaror.com - iziananatt Gymnsypeshy http://gusannghor.com Icercefaree
Add new comment