W3cubDocs

/Drupal 8

public function ModuleHandler::alter

public ModuleHandler::alter($type, &$data, &$context1 = NULL, &$context2 = NULL)

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 = array(
    'alterable' => &$alterable,
    'unalterable' => $unalterable,
    'foo' => 'bar',
  );
  $this->alter('mymodule_data', $alterable1, $alterable2, $context);

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

  $context = array(
    'unalterable_object' => clone $object,
  );
  $this->alter('mymodule_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 array('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

File

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

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 array('form', 'form_FORM_ID').
  if (is_array($type)) {
    $cid = implode(',', $type);
    $extra_types = $type;
    $type = array_shift($extra_types);
    // Allow if statements in this function to use the faster isset() rather
    // than !empty() both when $type is passed as a string, or as an array
    // with one item.
    if (empty($extra_types)) {
      unset($extra_types);
    }
  }
  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->alterFunctions[$cid])) {
    $this->alterFunctions[$cid] = array();
    $hook = $type . '_alter';
    $modules = $this->getImplementations($hook);
    if (!isset($extra_types)) {
      // For the more common case of a single hook, we do not need to call
      // function_exists(), since $this->getImplementations() returns only
      // modules with implementations.
      foreach ($modules as $module) {
        $this->alterFunctions[$cid][] = $module . '_' . $hook;
      }
    }
    else {
      // For multiple hooks, we need $modules to contain every module that
      // implements at least one of them.
      $extra_modules = array();
      foreach ($extra_types as $extra_type) {
        $extra_modules = array_merge($extra_modules, $this->getImplementations($extra_type . '_alter'));
      }
      // If any modules implement one of the extra hooks that do not implement
      // the primary hook, we need to add them to the $modules array in their
      // appropriate order. $this->getImplementations() can only return
      // ordered implementations of a single hook. To get the ordered
      // implementations of multiple hooks, we mimic the
      // $this->getImplementations() logic of first ordering by
      // $this->getModuleList(), and then calling
      // $this->alter('module_implements').
      if (array_diff($extra_modules, $modules)) {
        // Merge the arrays and order by getModuleList().
        $modules = array_intersect(array_keys($this->moduleList), array_merge($modules, $extra_modules));
        // Since $this->getImplementations() already took care of loading the
        // necessary include files, we can safely pass FALSE for the array
        // values.
        $implementations = array_fill_keys($modules, FALSE);
        // Let modules adjust the order solely based on the primary hook. This
        // ensures the same module order regardless of whether this if block
        // runs. Calling $this->alter() recursively in this way does not
        // result in an infinite loop, because this call is for a single
        // $type, so we won't end up in this code block again.
        $this->alter('module_implements', $implementations, $hook);
        $modules = array_keys($implementations);
      }
      foreach ($modules as $module) {
        // Since $modules is a merged array, for any given module, we do not
        // know whether it has any particular implementation, so we need a
        // function_exists().
        $function = $module . '_' . $hook;
        if (function_exists($function)) {
          $this->alterFunctions[$cid][] = $function;
        }
        foreach ($extra_types as $extra_type) {
          $function = $module . '_' . $extra_type . '_alter';
          if (function_exists($function)) {
            $this->alterFunctions[$cid][] = $function;
          }
        }
      }
    }
  }

  foreach ($this->alterFunctions[$cid] as $function) {
    $function($data, $context1, $context2);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!ModuleHandler.php/function/ModuleHandler::alter/8.1.x