Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Extension/ModuleHandler.php \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
  2. 9 core/lib/Drupal/Core/Extension/ModuleHandler.php \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()

Builds hook implementation information for a given hook name.

Parameters

string $hook: The name of the hook (e.g. "help" or "menu").

Return value

mixed[] An array whose keys are the names of the modules which are implementing this hook and whose values are either a string identifying a file in which the implementation is to be found, or FALSE, if the implementation is in the module file.

Throws

\RuntimeException Exception thrown when an invalid implementation is added by hook_module_implements_alter().

See also

\Drupal\Core\Extension\ModuleHandler::getImplementationInfo()

1 call to ModuleHandler::buildImplementationInfo()
ModuleHandler::getImplementationInfo in core/lib/Drupal/Core/Extension/ModuleHandler.php
Provides information about modules' implementations of a hook.

File

core/lib/Drupal/Core/Extension/ModuleHandler.php, line 637

Class

ModuleHandler
Class that manages modules in a Drupal installation.

Namespace

Drupal\Core\Extension

Code

protected function buildImplementationInfo($hook) {
  $implementations = [];
  $hook_info = $this
    ->getHookInfo();
  foreach ($this->moduleList as $module => $extension) {
    $include_file = isset($hook_info[$hook]['group']) && $this
      ->loadInclude($module, 'inc', $module . '.' . $hook_info[$hook]['group']);

    // Since $this->implementsHook() may needlessly try to load the include
    // file again, function_exists() is used directly here.
    if (function_exists($module . '_' . $hook)) {
      $implementations[$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
    }
  }

  // Allow modules to change the weight of specific implementations, but avoid
  // an infinite loop.
  if ($hook != 'module_implements_alter') {

    // Remember the original implementations, before they are modified with
    // hook_module_implements_alter().
    $implementations_before = $implementations;

    // Verify implementations that were added or modified.
    $this
      ->alter('module_implements', $implementations, $hook);

    // Verify new or modified implementations.
    foreach (array_diff_assoc($implementations, $implementations_before) as $module => $group) {

      // If an implementation of hook_module_implements_alter() changed or
      // added a group, the respective file needs to be included.
      if ($group) {
        $this
          ->loadInclude($module, 'inc', "{$module}.{$group}");
      }

      // If a new implementation was added, verify that the function exists.
      if (!function_exists($module . '_' . $hook)) {
        throw new \RuntimeException("An invalid implementation {$module}_{$hook} was added by hook_module_implements_alter()");
      }
    }
  }
  return $implementations;
}