I've created a new block and put my secondary links into it.

How do I display/use this block in my template?


marcvangend’s picture

- add a region to your template
- assign the block to this region in the drupal back-end on /admin/build/blocks
- read this for more info: http://drupal.org/node/171224

ica’s picture

generic snippet

$block = module_invoke('module_name', 'block', 'view', 0);
print $block['content'];

i.e logintoboggan block in a theme.tpl.php

<div class="container">
  <div id="header">	
		<div id="loginblock">
		$block = module_invoke('logintoboggan', 'block', 'view', 0);
		print $block['content'];
marcvangend’s picture

Funny... consider this as the community saying 'Welcome to Drupal!'
As you see above, there is often more than one way to do something. That's the kind of flexibility Drupal offers. Let's explain the difference between the solutions, so you don't get lost:
If you use the method with a region, a user with the right permissions can change the settings of the block in the back end. This includes changing the block title, placing it in another region, hiding it on specific pages or even disabling it completely. The module_invoke method loads the block directly into the template, so you cannot adjust settings for this block in the back end.

El Bandito’s picture

Can some clever person explain what the third and fourth arguments of this module invoke call do ? I have looked at the API def for module_invoke but unfortunately I remain unenlightened !



marcvangend’s picture

At http://api.drupal.org/api/function/module_invoke/6 you can read


$module The name of the module (without the .module extension).

$hook The name of the hook to invoke.

... Arguments to pass to the hook implementation.

This means that the first argument is the module name, the second argument is the hook name, all other arguments are passed on as parameters to the hook function that is being called.

Shai’s picture

Go to the block administration page and hover over one of the "configure" links (or click on configure). The second to last argument of the URL is the "module" name that created the block and the last argument is for the fourth parameter. So for instance, I hover the "configure" link for a block I want to invoke on a template. I see the following path:


From this I learn that the first parameter should be "webformblock" and the last parameter would be "71".

So back to the example

$block = module_invoke('module_name', 'block', 'view', '71');
print $block['content'];

I fill it in for my case like this:

$block = module_invoke('webformblock', 'block', 'view', '71');
print $block['content'];

In this case the last argument is an integer. But it is not always the case. For example, the Views module creates blocks and places a string for that parameter like, "contentlist-block_1".

Another example then might be:

$block = module_invoke('views', 'block', 'view', 'contentlist-block_1');
print $block['content'];

And yes, the stuff at api.drupal.org is very hard to decipher with examples.

VTM’s picture

I want the block only in the full node display.
I know I can do CSS but that way is not efficient (resources-wise).

marcvangend’s picture

Edit the block visibility settings on the block configure page.

VTM’s picture

I put this under "display in all pages except ..." : category/*/* and category/* but neither of them worked.

Question: Is it correct that invoking a block, bypasses it's display setting?
My php script is in node.tpl.php.

There is the possibility to add IF condition to the script so it will be activate only in FULL NODE display.
I just do not speak "Druplaish" very well ...

marcvangend’s picture

It would help if you would show your code, so I can see what exactly you are doing. Do I understand correctly that you are trying to render a block in node.tpl.php, instead of the usual page.tpl.php?

That said... Yes, if you directly invoke a block (using code like $block = module_invoke($module, 'block', 'view', $delta);), the visibility settings are not taken into account yet. The visibility settings do get applied when you call the theme_blocks function (see http://api.drupal.org/api/function/theme_blocks/6) which in its turn calls block_list (that's where the visibility settings are actualle handled, see http://api.drupal.org/api/function/block_list/6). What I would do is create a new region in your theme, called for instance 'in_node'. Then in your template.php you can put the following code:

THEMENAME_preprocess_node(&$variables) { // replace THEMENAME with the name of your theme
  $variables['in_node'] = theme('blocks', 'in_node');

If that _preprocess_node function already exists, just add that single line of code to it. This will make a new variable, $in_node, available in your node.tpl.php. Now in your node.tpl.php you can simply do <?php print $in_node; ?>.

PS. this is all untested code, please tell me (and forgive me) if I made an error.

VTM’s picture

First - you are right - I should have quoted the code but you corectly guessed: I used this code.

    $block = module_invoke('block', 'block', 'view', '10');
    print $block['content'];

Your suggestion did the job, with a small correction:

function THEMENAME_preprocess_node(&$vars, $hook) { // replace THEMENAME with the name of your theme
    $vars['REGIONNAME'] = theme('blocks', 'REGIONNAME');  // replace REGIONNAME with the name of your new region

The rest is as you described it.

marcvangend’s picture

Wonderful. Thanks for the feedback.

gurpreet2000’s picture

thank you.... its work for me

    $block = module_invoke('block', 'block', 'view', '10');
    print $block['content'];
chintan4u’s picture

$block = module_invoke('modulename', 'block_view', 'block_name');
print render($block);

Chintan Umarani
Drupal Developer

kimberlydb’s picture

I couldn't get the above to work, the solution I found is:

$block = block_load('views', 'block_name');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;

squarecandy’s picture

$block = module_invoke('views', 'block_view', 'my_viewname-block');
print render($block);

is working for me for views blocks...
be sure to go to the blocks admin page and get the machine name for the block from the configure URL.

Anonymous’s picture

Try category*

You can also set block views in context.

jovial_prash’s picture

$block = module_invoke($module_name, 'block', 'view', $delta);

Try above solution.
here '$module_name' and '$delta' information u can get by hovering over the 'configuration' link just besides that block in 'admin/build/block' page

srikanth.g’s picture

This code is working perfectly for me: https://www.drupal.org/project/addthis
$addblock = module_invoke('addthis', 'block_view', 'addthis_block');
print render($addblock['content']);