1 /** 2 * Invokes a hook in all enabled modules that implement it. 3 * 4 * All arguments are passed by value. Use drupal_alter() if you need to pass 5 * arguments by reference. 6 * 7 * @param $hook 8 * The name of the hook to invoke. 9 * @param ... 10 * Arguments to pass to the hook. 11 * 12 * @return 13 * An array of return values of the hook implementations. If modules return 14 * arrays from their implementations, those are merged into one array. 15 * 16 * @see drupal_alter() 17 */ 18 function module_invoke_all($hook) { 19 $args = func_get_args(); 20 // Remove $hook from the arguments. 21 unset($args[0]); 22 $return = array(); 23 foreach (module_implements($hook) as $module) { 24 $function = $module . '_' . $hook; 25 if (function_exists($function)) { 26 $result = call_user_func_array($function, $args); 27 if (isset($result) && is_array($result)) { 28 $return = array_merge_recursive($return, $result); 29 } 30 elseif (isset($result)) { 31 $return[] = $result; 32 } 33 } 34 } 35 36 return $return; 37 }
函数module_invoke_all()调用指定的钩子函数。例如stream_wappers钩子,假设有模块a和b都有实现该钩子,则会执行函数a_stream_wrappers()和b_stream_wrappers()。
函数module_invoke_all()所有参数都是传值的,如果需要传址参数,则需要使用函数drupal_alter()代替。
函数module_invoke_all()返回一个数组,上例的stream_wrappers钩子假设模块a和b都有返回值,则返回的结果应该是{a_stream_wrappers()结果,b_stream_wrappers()结果}。函数drupal_alter()没有返回值。