Skip to content

Instantly share code, notes, and snippets.

@nalipaz
Created April 29, 2015 19:57
Show Gist options
  • Save nalipaz/152c3bd2089b22271eb5 to your computer and use it in GitHub Desktop.
Save nalipaz/152c3bd2089b22271eb5 to your computer and use it in GitHub Desktop.
Messaging API, custom module_invoke_all()
<?php
/**
* Invoke hook on all modules
*
* This is like module_invoke_all with some differences:
* - The results are just merged (not recursively)
* - The module name is added to each resulting array
*
* @param $hook
* The name of the hook to invoke.
* @param ...
* Arguments to pass to the hook.
* @return
* An array of return values of the hook implementations. If modules return
* arrays from their implementations, those are merged into one array.
*/
function messaging_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)) {
foreach ($result as $key => &$value) {
if (is_array($value)) {
$value += array('module' => $module);
}
}
$return = array_merge($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment