4.6.x module.inc module_invoke()
4.7.x module.inc module_invoke()
5.x module.inc module_invoke()
6.x module.inc module_invoke()
7.x module.inc module_invoke($module, $hook)

Invokes a hook in a particular module.

All arguments are passed by value. Use drupal_alter() if you need to pass arguments by reference.

Parameters

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

Return value

The return value of the hook implementation.

See also

drupal_alter()

Related topics

70 calls to module_invoke()
aggregator_admin_form in modules/aggregator/aggregator.admin.inc
Form constructor for the aggregator system settings.
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
aggregator_refresh in modules/aggregator/aggregator.module
Checks a news feed for new items.
block_admin_configure in modules/block/block.admin.inc
Form constructor for the block configuration form.
block_admin_configure_submit in modules/block/block.admin.inc
Form submission handler for block_admin_configure().

... See full list

File

includes/module.inc, line 921
API for loading and interacting with Drupal modules.

Code

function module_invoke($module, $hook) {
  $args = func_get_args();
  // Remove $module and $hook from the arguments.
  unset($args[0], $args[1]);
  if (module_hook($module, $hook)) {
    return call_user_func_array($module . '_' . $hook, $args);
  }
}

Comments

Jānis Bebrītis’s picture

if you want to get views blocks in drupal 7, you have to use

$block = module_invoke('views', 'block_view', 'map-block_1');

on drupal 6 it was

$block = module_invoke('views', 'block', 'view', 'map-block_1');
Ericmaster’s picture

as an alternative, if the display is set as a block I prefer to use

http://api.drupal.org/api/drupal/modules--block--block.module/function/b...

the advantage is that this function will include the contextual links

m.sant’s picture

In this way the title will not be included.

rajatgusain’s picture

For Drupal 7 :

$block = module_invoke('block', 'block_view', 30);

@arg 1 : module name
@arg 2 : hook name like block_view, block_info
@arg 3 : id or delta of the block e.g 30, map-block_1

for Drupal 6 :

$block = module_invoke('block', 'block', 'view', 30);

@arg 1 : module name
@arg 2 : hook name like block
@arg 3 : $op of hook_block e.g. info, view, configure
@arg 4 : id or delta of the block e.g 30, map-block_1

featherbelly’s picture

e.g. to display block:

  print render(module_invoke( 'module_name', 'hook_name', 'block_delta'));
rksadit’s picture

Im using the same method to copy the 'search_api_saved_search' module block , it is working fine. The problem is whenever i save the search in copied block, the success_message will appear in the original block section. Any idea how to overcome this problem.

feo’s picture

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);
  return $block_rendered;
}
prabhatjn’s picture

The above function works smoothly.
Thanks for sharing.
Perry.

Nikdilis’s picture

$block = block_load('block', '5');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
dswans’s picture

Works incredibly well for normal stuff but cant get it to render vews for some reason?

barraponto’s picture

@dswans you have to pass the views display name as a third argument, see the first comment.

m.sant’s picture

The function block_render apprently works and correctly display the title of the block, but it causes a lot of Notices:

Notice: Undefined property: stdClass::$title in _block_render_blocks() (line 856 di /home/.../modules/block/block.module).
Notice: Undefined property: stdClass::$region in template_preprocess_block() (line 939 of /home/.../modules/block/block.module).
Notice: Undefined property: stdClass::$region in template_preprocess_block() (line 940 of /home/.../modules/block/block.module).
Notice: Undefined property: stdClass::$region in template_preprocess_block() (line 943 of /home/.../modules/block/block.module).
Notice: Undefined property: stdClass::$region in template_preprocess_block() (line 944 of /home/.../modules/block/block.module).
Notice: Undefined property: stdClass::$region in template_preprocess_block() (line 951 of /home/.../modules/block/block.module).

matiki’s picture

yeah same problem here.

_redfog’s picture

I got the same notices. They appear because when you call block_load with an unknown delta, it creates a default block object only with the module name + the delta (so without $title and $region).

rafinskipg’s picture

Perfect function for my preprocess functions. Love it, thanks

jackocnr’s picture

@feo thanks a lot - this is perfect - with the block title and the contextual links and everything!

prabhatjn’s picture

Just use it like this (for your custom block for instance):
echo block_render('block', 13);

P.

Nikdilis’s picture

//print out block 26
$block = module_invoke('block', 'block_view', 26);
print $block['content'];
m.sant’s picture

Doesn't work in D7

Taiger’s picture

This is for Drupal 7.

//print out block 26
$block = module_invoke('block', 'block_view', 26);
print render($block['content']['#content']);
//dpm($block); //uncomment to view with devel enabled

You can find the correct module name and block id from the url on the configure link on the block administration page.

EllECTRONC’s picture

It works for D7

sammyframson’s picture

Worked in d7 for me (http://api.drupal.org/comment/reply/22770/17559#comment-17559). After many try's with a variety of functions, the only one that I could get to work in my node--content_type.tpl.php was this code. I was trying to render a gmap location block into the content page and this is what finally worked (D7.9):

<?php
$block = module_invoke('gmap_location', 'block_view', 0);
print $block['content'];
?>

Thanks.

aendrew’s picture

This worked for me in D7 when trying to display my custom module inside of a PHP text field in a Views header.

jkohtama’s picture

I could not get block template overrides working with the 'module_invoke'-using code that is listed above and in lots of other pages. Searched for a while and stumbled upon this page. The block_load method listed here works smoothly.

mstrom81’s picture

Hi there,

Will the module_invoke be called for all users of the site, or is there a way to only get the module_invoke to be called for a particular user using params?

Thanks very much.

EllECTRONC’s picture

Show search block anywhere (delta not needed):

$block = module_invoke('search', 'block_view');
print render($block['content']);

Show views's block which name is 'comments_recent':

$block = module_invoke('views', 'block_view', 'comments_recent-block');
print render($block['content']);
seaji’s picture

How to use arguments in contextual filters in views?

print render(module_invoke('views', 'block_view', 'DELTA', 'ARGUMENT'));

Not working

pixelmord’s picture

or other display, you can use this:

views_embed_view($name, $display_id = 'default');

You can also pass additional arguments e.g. for contextual filters like:

views_embed_view('view_name', 'block_display_id', 'argument');

See:
http://drupalcontrib.org/api/drupal/contributions--views--views.module/f...

yaworsk’s picture

just a quick note - if you are using this in a tpl file, you'll want to have

<?php
print render (views_embed_view('view_name', 'block_display_id', 'argument'));
?>
mototribe’s picture

that was the best way for me to embed a view in D7

IamOnStage’s picture

<?php	$block = module_invoke('webform', 'block_view', 'client-block-167');
				print render($block['content']); ?>

Prints the block but this prints it using the node.tpl not the system block view.

Any way to print as a system block?

beanluc’s picture

Rendering the whole $block and not just its content should work.

Madhuri-a’s picture

Thanks!! this function works for me.

aendrew’s picture

If wanting to embed a Mini-Panel into a template, try the following:

$block = module_invoke('panels_mini', 'block_view', 'mini_panel_machine_name');
print $block['content'];

Took me awhile to figure out and couldn't find much in the way of good info on doing so online.

zunaeid’s picture

I thing this is better


$block = block_load('MODULE_NAME', 'DELTA'); 
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));

For views here
MODULE_NAME = "views";
DELTA = "VIEWS_NAME-DISPLAY_ID"

$block = block_load('views', 'related_courses-block_1');
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))))
T1ckL35’s picture

For reference I was using:

$block = module_invoke('block', 'block_view', '6');
print render($block['content']);

Which seemed to work fine until I tried to implement a hook_block_view_block_6_alter() function. When I did this the hook function was never called. I imagine down the call stack the hook is never called properly using module_invoke...
So I tried:

$block = block_load('block', $blockName); 
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));

which does run the hook function as expected. One to note.

T1ckL35’s picture

I've also noticed that for me, calling block_load (the second method) means that the interface renders out the block admin dropdown (edit->view and configure->block links) when logged in as an admin. When using the first method (module_invoke) this is not displayed.

Chetna_Negi’s picture

Thanks for your comment, I wanted to print a module's block, and this code worked absolutely fine.

miguelfrias’s picture

Emphasizing what Taiger said:
You can find the correct module name and block id from the url on the configure link on the block administration page.

Nathan Goulding’s picture

This has to go down in history as the greatest Drupal function ever. It provides literally no benefit. Why not just call the module's function directly? Is this function going to be expanded in the future to do something awesome? Lawl.

qqboy’s picture

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);
return $block_rendered;
}

print block_render('menu_block',1);
print block_render('menu_block',2);
print block_render('menu_block',3);

for your info:
module name and id both can be found in the block configure link on block administration page.

joachim’s picture

ptocco’s picture

I'm flabbergasted at how many answers there are to printing a view/block within a template file. Why no single definitive answer? Maybe because excessive manipulation at the template level is not what the original Drupal 7 authors had in mind, yet this seems to be a popular strategy among advanced themers.

This is all I needed to add for my block to display in my custom page.tpl file:

<?php
$view = views_get_view('my_view_name');
print $view->preview('block_1');
?>

This prints the block version of my view. (You have to add a block version to your view for this to work.)

I got the tip from this link:

http://lhmdesign.com/posts/embed-drupal-views-using-php