module_invoke_all

5 module.inc module_invoke_all()
6 module.inc module_invoke_all()
7 module.inc module_invoke_all($hook)
8 module.inc module_invoke_all($hook)

Invoke a hook in all enabled modules that implement it.

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.

Related topics

44 calls to module_invoke_all()

File

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

Code

function module_invoke_all() {
  $args = func_get_args();
  $hook = $args[0];
  unset($args[0]);
  $return = array();
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    $result = call_user_func_array($function, $args);
    if (isset($result) && is_array($result)) {
      $return = array_merge_recursive($return, $result);
    }
    else if (isset($result)) {
      $return[] = $result;
    }
  }

  return $return;
}

Comments

neater

I would replace

<?php
function module_invoke_all() {
 
$args = func_get_args();
 
$hook = $args[0];
  unset(
$args[0]);
?>

with

<?php
function module_invoke_all() {
 
$args = func_get_args();
 
$hook = array_shift($args);
?>

or

<?php
function module_invoke_all($hook) {
 
$args = func_get_args();
  unset(
$args[0]);
?>

Please file a patch (or even

Please file a patch (or even just an issue) for this. Nothing will get done by just talking in these comments! :)

Here's the documentation on getting involved in developing Drupal core.

references

module_invoke_all() doesn't support references.
See: [#353494]
http://drupal.org/node/353494

If you need to execute the hook through references, you can use this code instead:

<?php
  $hook
= 'my_hook';
  foreach (
module_implements($hook) as $module) {
   
$function = $module . '_' . $hook;
   
$function($term);
  }
?>

Another approach to references

PHP 5.3 (5.2?), changed the way that call_user_func_array() treats args passed by reference.

Here's how I've modified module_invoke_all() to deal with this:

<?php
function module_invoke_all() {
 
$args = func_get_args();
 
$hook = $args[0];
  unset(
$args[0]);
 
$return = array();
 
$lOldPHP = (strnatcmp(phpversion(),'5.2') <= 0);
  foreach (
module_implements($hook) as $module) {
   
$function = $module .'_'. $hook;
    if (
$lOldPHP)
     
$result = call_user_func_array($function, $args);
    else {
     
$ref_args = array();
      foreach (
$args as $key => &$arg) {
       
$ref_args[$key] = &$arg;
      }
     
$result = call_user_func_array($function, $ref_args);
    }
    if (isset(
$result) && is_array($result)) {
     
$return = array_merge_recursive($return, $result);
    }
    else if (isset(
$result)) {
     
$return[] = $result;
    }
  }

  return
$return;
}
?>

The function module_invoke() needs a similar change.

[Follow-up:] This is not actually a good fix. See http://drupal.org/node/353494

Login or register to post comments