Definition

module_implements($hook, $sort = FALSE)
drupal/includes/module.inc, line 419

Description

Determine which modules are implementing a hook.

Parameters

$hook The name of the hook (e.g. "help" or "menu"). Special cases: MODULE_IMPLEMENTS_CLEAR_CACHE: Force the stored list of hook implementations to be regenerated (such as after enabling a new module, before processing hook_enable). MODULE_IMPLEMENTS_WRITE_CACHE: Write the stored list of hook implementations into the cache_registry table.

$sort By default, modules are ordered by weight and filename. By setting this option to TRUE, modules will be ordered by module name.

Return value

An array with the names of the modules which are implementing this hook. All enabled modules are taken into consideration and the files containing the implementations are loaded as necessary.

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

function module_implements($hook, $sort = FALSE) {
  static $implementations = array(), $sorted_implementations = array(), $loaded = array(), $cached_hooks = 0;

  if (defined('MAINTENANCE_MODE')) {
    return _module_implements_maintenance($hook, $sort);
  }
  if ($hook === MODULE_IMPLEMENTS_CLEAR_CACHE) {
    $implementations = array();
    $sorted_implementations = array();
    $loaded = array();
    $cached_hooks = 0;
    cache_clear_all('hooks', 'cache_registry');
    return;
  }
  if ($hook === MODULE_IMPLEMENTS_WRITE_CACHE) {
    // Only write this to cache if we loaded new implementations.
    if (count($implementations) > $cached_hooks) {
      cache_set('hooks', $implementations, 'cache_registry');
    }
    return;
  }

  if (!isset($loaded[$hook])) {
    if (empty($implementations) && ($cache = cache_get('hooks', 'cache_registry'))) {
      $implementations = $cache->data;
      $cached_hooks = count($implementations);
    }
    if (!isset($implementations[$hook])) {
      $implementations[$hook] = db_query("SELECT module FROM {registry} WHERE type = 'function' AND suffix = :hook ORDER BY weight, module", array(':hook' => $hook))->fetchCol();
    }
    foreach ($implementations[$hook] as $module) {
      $function = $module . '_' . $hook;
      if (!function_exists($function)) {
        drupal_function_exists($function);
      }
    }
    $loaded[$hook] = TRUE;
  }

  if ($sort) {
    if (!isset($sorted_implementations[$hook])) {
      $sorted_implementations[$hook] = $implementations[$hook];
      sort($sorted_implementations[$hook]);
    }
    return $sorted_implementations[$hook];
  }
  else {
    return $implementations[$hook];
  }
}