function ModuleHandler::invoke

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

Invokes a hook in a particular module.

This will execute multiple implementations as long as all returned values are void or an array. Arrays will be merged with NestedArray::mergeDeep.

Parameters

string $module: The name of the module (without the .module extension).

string $hook: The name of the hook to invoke.

array $args: Arguments to pass to the hook implementation.

Return value

mixed The return value of the hook implementation.

Overrides ModuleHandlerInterface::invoke

1 call to ModuleHandler::invoke()
ModuleHandler::invokeDeprecated in core/lib/Drupal/Core/Extension/ModuleHandler.php

File

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

Class

ModuleHandler
Class that manages modules in a Drupal installation.

Namespace

Drupal\Core\Extension

Code

public function invoke($module, $hook, array $args = []) {
  $list = $this->getHookImplementationList($hook);
  $listeners = $list->getForModule($module);
  if ($listeners) {
    if (count($listeners) > 1) {
      $results = [];
      foreach ($listeners as $listener) {
        $result = $listener(...$args);
        if ($result === NULL) {
          continue;
        }
        if (!is_array($result) && !$result instanceof \ArrayObject) {
          // If the result is not an array and there is more than one
          // implementation for the module we cannot merge the results.
          throw new \LogicException("Module {$module} should not implement {$hook} more than once.");
        }
        $results = NestedArray::mergeDeep($results, $result);
      }
      return $results;
    }
    return reset($listeners)(...$args);
  }
  return $this->legacyInvoke($module, $hook, $args);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.