7 module.inc | module_invoke( |
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
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().
File
- includes/
module.inc, line 864 - 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
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?
@dswans you have to pass the
@dswans you have to pass the views display name as a third argument, see the first comment.
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
Thanks
@feo thanks a lot - this is perfect - with the block title and the contextual links and everything!
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.
This worked for me in D7 when
This worked for me in D7 when trying to display my custom module inside of a PHP text field in a Views header.
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...
just a quick note - if you
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'));
?>
Thank you
that was the best way for me to embed a view in D7
Not printing System Block View
<?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?
Render the whole $block
Rendering the whole $block and not just its content should work.
Thanks!! this function works
Thanks!! this function works for me.
Mini-Panels in D7
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.
To render views block in Drupal 7
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 views
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.
wow
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.