function module_invoke

You are here

7 module.inc module_invoke($module, $hook)
4.6 module.inc module_invoke()
4.7 module.inc module_invoke()
5 module.inc module_invoke()
6 module.inc module_invoke()
8 bootstrap.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 861
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

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

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

In this way the title will not be included.

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

e.g. to display block:

<?php
 
print render(module_invoke( 'module_name', 'hook_name', 'block_delta'));
?>

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

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

<?php
$block
= block_load('block', '5');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print
$output;
?>

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

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

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

yeah same problem here.

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

Perfect function for my preprocess functions. Love it, thanks

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

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

P.

<?php
//print out block 26
$block = module_invoke('block', 'block_view', 26);
print
$block['content'];
?>

Doesn't work in D7

This is for Drupal 7.

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

It works for D7

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.

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

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.

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.

Show search block anywhere (delta not needed):

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

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

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

How to use arguments in contextual filters in views?

<?php
print render(module_invoke('views', 'block_view', 'DELTA', 'ARGUMENT'));
?>

Not working

or other display, you can use this:

<?php
views_embed_view
($name, $display_id = 'default');
?>

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

<?php
views_embed_view
('view_name', 'block_display_id', 'argument');
?>

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

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

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

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

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

Thanks!! this function works for me.

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

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

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

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.

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.

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.

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.