Same name and namespace in other branches
- 4.6.x includes/module.inc \module_invoke_all()
- 4.7.x includes/module.inc \module_invoke_all()
- 5.x includes/module.inc \module_invoke_all()
- 6.x includes/module.inc \module_invoke_all()
Invokes a hook in all enabled modules that implement it.
All arguments are passed by value. Use drupal_alter() if you need to pass arguments by reference.
Parameters
$hook: The name of the hook to invoke.
...: Arguments to pass to the hook.
Return value
An array of return values of the hook implementations. If modules return arrays from their implementations, those are merged into one array recursively. Note: integer keys in arrays will be lost, as the merge is done using array_merge_recursive().
See also
Related topics
146 calls to module_invoke_all()
- actions_delete in includes/
actions.inc - Deletes a single action from the database.
- actions_list in includes/
actions.inc - Discovers all available actions by invoking hook_action_info().
- aggregator_remove in modules/
aggregator/ aggregator.module - Removes all items from a feed.
- ajax_footer in includes/
ajax.inc - Performs end-of-Ajax-request tasks.
- archiver_get_info in includes/
common.inc - Retrieves a list of all available archivers.
1 string reference to 'module_invoke_all'
- watchdog in includes/
bootstrap.inc - Logs a system message.
File
- includes/
module.inc, line 958 - API for loading and interacting with Drupal modules.
Code
function module_invoke_all($hook) {
$args = func_get_args();
// Remove $hook from the arguments.
unset($args[0]);
$return = array();
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
if (function_exists($function)) {
$result = call_user_func_array($function, $args);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
elseif (isset($result)) {
$return[] = $result;
}
}
}
return $return;
}
Comments
Pass by reference
Unfortunately this function and module_invoke() do not allow passing variables by reference.
To work around this, you can do something like this:
And, make sure to have the ampersand in the hook declaration:
drupal_alter()?
If you want to pass things by reference, it's likely that you want to do
$modified_data = drupal_alter('my_type', $data);instead.
http://api.drupal.org/api/drupal/includes!module.inc/function/drupal_alt...
Don't assign when passing by reference.
I'm not disagreeing with the above, but take care; `drupal_alter` hook implementations typically do not return a value (because the data is passed by reference, it is modified in place without returning). (The code example above is assigning the return value of the drupal alter hook to a variable.)
One exception to pass by reference...
There is an exception to passing by reference. If the arguments for the hook are Objects, then they are passed by reference. This is default behaviour of php:
http://php.net/manual/en/language.oop5.references.php .
An example of this in action is with the feeds hook hook_feeds_after_parse()
http://drupalcontrib.org/api/drupal/contributions!feeds!feeds.api.php/fu...
The hook is called using module_invoke_all, but you can, for example, remove all the
$results->itemsand the feeds import won't do anything.
If you really want to avoid a
If you really want to avoid a hook from altering your object, you may clone the object first as described in drupal_alter.
module_invoke_all('mydata_insert', clone $data_object);But again, be careful here.
But again, be careful here. This is not a deep clone, so any child objects in objects, arrays, etc. could be altered.
Here are some links to how to
Here are some links to how to achieve this in Drupal 8:
Replace all module_invoke_all() deprecated function calls in procedural code.
More links explaining why it was removed in D8
D8 equivalent example
D7:
D8: