function ModuleHandler::alter

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

Passes alterable variables to specific hook_TYPE_alter() implementations.

This dispatch function hands off the passed-in variables to type-specific hook_TYPE_alter() implementations in modules. It ensures a consistent interface for all altering operations.

A maximum of 2 alterable arguments is supported. In case more arguments need to be passed and alterable, modules provide additional variables assigned by reference in the last $context argument:


  $context = [
    'alterable' => &$alterable,
    'unalterable' => $unalterable,
    'foo' => 'bar',
  );
  $this->alter('my_module_data', $alterable1, $alterable2, $context);

Note that objects are always passed by reference. If it is absolutely required that no implementation alters a passed object in $context, then an object needs to be cloned:


  $context = [
    'unalterable_object' => clone $object,
  );
  $this->alter('my_module_data', $data, $context);

Parameters

string|array $type: A string describing the type of the alterable $data. 'form', 'links', 'node_content', and so on are several examples. Alternatively can be an array, in which case hook_TYPE_alter() is invoked for each value in the array, ordered first by module, and then for each module, in the order of values in $type. For example, when Form API is using $this->alter() to execute both hook_form_alter() and hook_form_FORM_ID_alter() implementations, it passes ['form', 'form_' . $form_id] for $type.

mixed $data: The variable that will be passed to hook_TYPE_alter() implementations to be altered. The type of this variable depends on the value of the $type argument. For example, when altering a 'form', $data will be a structured array. When altering a 'profile', $data will be an object.

mixed $context1: (optional) An additional variable that is passed by reference.

mixed $context2: (optional) An additional variable that is passed by reference. If more context needs to be provided to implementations, then this should be an associative array as described above.

Overrides ModuleHandlerInterface::alter

2 calls to ModuleHandler::alter()
ModuleHandler::alterDeprecated in core/lib/Drupal/Core/Extension/ModuleHandler.php
Passes alterable variables to deprecated hook_TYPE_alter() implementations.
ModuleHandler::reOrderModulesForAlter in core/lib/Drupal/Core/Extension/ModuleHandler.php
Reorder modules for alters.

File

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

Class

ModuleHandler
Class that manages modules in a Drupal installation.

Namespace

Drupal\Core\Extension

Code

public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {
  // Most of the time, $type is passed as a string, so for performance,
  // normalize it to that. When passed as an array, usually the first item in
  // the array is a generic type, and additional items in the array are more
  // specific variants of it, as in the case of ['form', 'form_FORM_ID'].
  if (is_array($type)) {
    $cid = implode(',', $type);
  }
  else {
    $cid = $type;
  }
  // Some alter hooks are invoked many times per page request, so store the
  // list of functions to call, and on subsequent calls, iterate through them
  // quickly.
  if (!isset($this->alterEventListeners[$cid])) {
    $hooks = is_array($type) ? array_map(static fn(string $type) => $type . '_alter', $type) : [
      $type . '_alter',
    ];
    $this->alterEventListeners[$cid] = $this->getCombinedListeners($hooks);
  }
  foreach ($this->alterEventListeners[$cid] as $listener) {
    $listener($data, $context1, $context2);
  }
}

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