diff --git a/includes/module.inc b/includes/module.inc index 5a04382..0c06609 100644 --- a/includes/module.inc +++ b/includes/module.inc @@ -676,6 +676,10 @@ function module_hook($module, $hook) { /** * Determines which modules are implementing a hook. * + * Lazy-loaded include files specified with "group" via hook_hook_info() or + * hook_module_implements_alter() will be automatically included as part of + * module_implements(*, *, FALSE). + * * @param string $hook * The name of the hook (e.g. "help" or "menu"). * @param bool $sort @@ -696,7 +700,7 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) { static $drupal_static_fast; if (!isset($drupal_static_fast)) { $drupal_static_fast['implementations'] = &drupal_static(__FUNCTION__); - $drupal_static_fast['verified'] = &drupal_static(__FUNCTION__ . '.verified'); + $drupal_static_fast['verified'] = &drupal_static(__FUNCTION__ . ':verified'); } $implementations = &$drupal_static_fast['implementations']; $verified = &$drupal_static_fast['verified']; @@ -722,6 +726,9 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) { } // Fetch implementations from cache. + // This happens on the first call to module_implements(*, *, FALSE) during a + // request, but also when $implementations have been reset, e.g. after + // module_enable(). if (empty($implementations)) { $implementations = cache_get('module_implements', 'cache_bootstrap'); if ($implementations === FALSE) { @@ -730,6 +737,10 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) { else { $implementations = $implementations->data; } + // Forget all previously "verified" hooks, in case that $implementations + // were cleared via drupal_static_reset('module_implements') instead of + // module_implements(*, *, TRUE). + $verified = array(); } if (!isset($implementations[$hook])) { @@ -748,20 +759,31 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) { $implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE; } } - // Allow modules to change the weight of specific implementations but avoid + // Allow modules to change the weight of specific implementations, but avoid // an infinite loop. if ($hook != 'module_implements_alter') { + // Remember the implementations before hook_module_implements_alter(). + $implementations_before = $implementations[$hook]; drupal_alter('module_implements', $implementations[$hook], $hook); + // Verify implementations that were added or modified. + foreach (array_diff_assoc($implementations[$hook], $implementations_before) as $module => $group) { + // If drupal_alter('module_implements') changed or added a $group, the + // respective file needs to be included. + if ($group) { + module_load_include('inc', $module, "$module.$group"); + } + // If a new implementation was added, verify that the function exists. + if (!function_exists($module . '_' . $hook)) { + unset($implementations[$hook][$module]); + } + } } + // Implementations for this hook are now "verified" + $verified[$hook] = TRUE; } - - // The first time a hook is used in a request, the implementations must be - // "verified" with function_exists(), and the file specified by $group must be - // included. - // This applies both to implementations loaded from cache, and implementations - // that were discovered just now and may have been changed with - // hook_module_implements_alter(). - if (empty($verified[$hook])) { + elseif (!isset($verified[$hook])) { + // Implementations for this hook were in the cache, but they are not + // "verified" yet. foreach ($implementations[$hook] as $module => $group) { // If this hook implementation is stored in a lazy-loaded file, so include // that file first.