5 module.inc | module_invoke() |
6 module.inc | module_invoke() |
7 module.inc | module_invoke($module, $hook) |
8 module.inc | module_invoke($module, $hook) |
Invoke a hook in a particular module.
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.
Related topics
70 calls to module_invoke()
File
- includes/
module.inc, line 790 - 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);
}
}
Login or register to post comments
Comments
display views blocks
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
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
In this way the title will not be included.
More specific detail for module
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:
e.g. to display block:
<?php
print render(module_invoke( 'module_name', 'hook_name', 'block_delta'));
?>
to render blocks in Drupal 7 better to use Block API
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
The above function works smoothly.
Thanks for sharing.
Perry.
Shorter
<?php
$block = block_load('block', '5');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
?>
For views
Works incredibly well for normal stuff but cant get it to render vews for some reason?
block_render causes notices
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.
yeah same problem here.
The same...
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
Perfect function for my preprocess functions. Love it, thanks
Better and even shorter
<?php
//print out block 26
$block = module_invoke('block', 'block_view', 26);
print $block['content'];
?>
Doesn't work in D7
Doesn't work in D7
Drupal 7
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.
works
It works for D7
"Better and even shorter" Worked in d7 for me
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.
module_invoke and block theme overriding
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.
Will this invoke a module for all users of site?
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.
Search block and views block (D7)
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
How to use arguments in contextual filters in views?
<?php
print render(module_invoke('views', 'block_view', 'DELTA', 'ARGUMENT'));
?>
Not working
if you want to render a views block
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...