You are here

public function Drupal7::drupal_alter in Service Container 7.2

Same name and namespace in other branches
  1. 7 src/Legacy/Drupal7.php \Drupal\service_container\Legacy\Drupal7::drupal_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 (a third is supported for legacy reasons, but should not be used in new code). 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',
);
drupal_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,
);
drupal_alter('mymodule_data', $data, $context);

Parameters

$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 drupal_alter() to execute both hook_form_alter() and hook_form_FORM_ID_alter() implementations, it passes array('form', 'form_' . $form_id) for $type.

$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.

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

$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.

$context3: (optional) An additional variable that is passed by reference. This parameter is deprecated and will not exist in Drupal 8; consequently, it should not be used for new Drupal 7 code either. It is here only for backwards compatibility with older code that passed additional arguments to drupal_alter().

File

src/Legacy/Drupal7.php, line 384
Contains \Drupal\service_container\Legacy\Drupal7.

Class

Drupal7
Defines the Drupal 7 legacy service.

Namespace

Drupal\service_container\Legacy

Code

public function drupal_alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) {
  drupal_alter($type, $data, $context1, $context2, $context3);
}