module_implements
- Versions
- 4.6
module_implements($hook)- 4.7
module_implements($hook, $sort = FALSE)- 5 – 6
module_implements($hook, $sort = FALSE,$refresh= FALSE)- 7
module_implements($hook, $sort = FALSE, $reset = FALSE)
Determine which modules are implementing a hook.
See also
module_implements_write_cache()
Parameters
$hook The name of the hook (e.g. "help" or "menu").
$sort By default, modules are ordered by weight and filename, settings this option to TRUE, module list will be ordered by module name.
$reset For internal use only: Whether to force the stored list of hook implementations to be regenerated (such as after enabling a new module, before processing hook_enable).
Return value
An array with the names of the modules which are implementing this hook.
Related topics
Code
includes/module.inc, line 368
<?php
function module_implements($hook, $sort = FALSE, $reset = FALSE) {
$implementations = &drupal_static(__FUNCTION__, array());
// We maintain a persistent cache of hook implementations in addition to the
// static cache to avoid looping through every module and every hook on each
// request. Benchmarks show that the benefit of this caching outweighs the
// additional database hit even when using the default database caching
// backend and only a small number of modules are enabled. The cost of the
// cache_get() is more or less constant and reduced further when non-database
// caching backends are used, so there will be more significant gains when a
// large number of modules are installed or hooks invoked, since this can
// quickly lead to module_hook() being called several thousand times
// per request.
if ($reset) {
$implementations = array();
cache_set('module_implements', array());
drupal_static_reset('module_hook_info');
drupal_static_reset('drupal_alter');
cache_clear_all('hook_info', 'cache');
return;
}
// Fetch implementations from cache.
if (empty($implementations)) {
$implementations = cache_get('module_implements');
if ($implementations === FALSE) {
$implementations = array();
}
else {
$implementations = $implementations->data;
}
}
if (!isset($implementations[$hook])) {
$hook_info = module_hook_info();
$implementations[$hook] = array();
$list = module_list(FALSE, FALSE, $sort);
foreach ($list as $module) {
$include_file = FALSE;
if (module_hook($module, $hook) || (isset($hook_info[$hook]['group']) && $include_file = module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']) && module_hook($module, $hook))) {
$implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
// We added something to the cache, so write it when we are done.
$implementations['#write_cache'] = TRUE;
}
}
}
else {
foreach ($implementations[$hook] as $module => $group) {
// If this hook implementation is stored in a lazy-loaded file, so include
// that file first.
if ($group) {
module_load_include('inc', $module, "$module.$group");
}
// It is possible that a module removed a hook implementation without the
// implementations cache being rebuilt yet, so we check module_hook() on
// each request to avoid undefined function errors.
if (!module_hook($module, $hook)) {
// Clear out the stale implementation from the cache and force a cache
// refresh to forget about no longer existing hook implementations.
unset($implementations[$hook][$module]);
$implementations['#write_cache'] = TRUE;
}
}
}
return array_keys($implementations[$hook]);
}
?>Login or register to post comments 