\module_invoke_all
function
Invokes a hook in all enabled modules that implement it.

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.

Comments

dwieeb’s picture

Unfortunately this function and module_invoke() do not allow passing variables by reference.

To work around this, you can do something like this:

foreach (module_implements('my_hook') as $module) {
  $function = $module . '_my_hook';
  $function($var1, $var2);
}

And, make sure to have the ampersand in the hook declaration:

function my_module_my_hook(&$var1, &$var2) {
  $var1 = 'do';
  $var2 = 'stuff';
}
dalin’s picture

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

aron.beal’s picture

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

drclaw’s picture

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

and the feeds import won't do anything.

ronyclau’s picture

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

Dubs’s picture

But again, be careful here. This is not a deep clone, so any child objects in objects, arrays, etc. could be altered.

alberto56’s picture

aaronbauman’s picture

D7:

module_invoke_all('node_view', $entity, $display, $entity_view_mode, $langcode);

D8:

\Drupal::moduleHandler()->invokeAll($view_hook, array($entity, $display, $entity_view_mode, $langcode));