function public static function Bootstrap::alter
8.x-3.x Bootstrap.php | public static Bootstrap::alter($function, &$data, &$context1 = NULL, &$context2 = NULL) |
Manages theme alter hooks as classes and allows sub-themes to sub-class.
Parameters
string $function: The procedural function name of the alter (e.g. FUNCTION).
mixed $data: The variable that was passed to the hook_TYPE_alter() implementation 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.
Class
- Bootstrap
- The primary class for the Drupal Bootstrap base theme.
Namespace
Drupal\bootstrapSource src/Bootstrap.php (line 220)
public static function alter($function, &$data, &$context1 = NULL, &$context2 = NULL) {
// Do not statically cache this as the active theme may change.
$theme = static::getTheme();
$theme_name = $theme->getName();
// Immediately return if the active theme is not Bootstrap based.
if (!$theme->isBootstrap()) {
return;
}
// Handle alter and form managers.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['alter_managers'] = &drupal_static(__METHOD__ . '__alterManagers', []);
$drupal_static_fast['form_managers'] = &drupal_static(__METHOD__ . '__formManagers', []);
}
/* @var \Drupal\bootstrap\Plugin\AlterManager[] $alter_managers */
$alter_managers = &$drupal_static_fast['alter_managers'];
if (!isset($alter_managers[$theme_name])) {
$alter_managers[$theme_name] = new AlterManager($theme);
}
/* @var \Drupal\bootstrap\Plugin\FormManager[] $form_managers */
$form_managers = &$drupal_static_fast['form_managers'];
if (!isset($form_managers[$theme_name])) {
$form_managers[$theme_name] = new FormManager($theme);
}
// Retrieve the alter and form managers for this theme.
$alter_manager = $alter_managers[$theme_name];
$form_manager = $form_managers[$theme_name];
// Extract the alter hook name.
$hook = Unicode::extractHook($function, 'alter');
// Handle form alters as a separate plugin.
if (strpos($hook, 'form') === 0 && $context1 instanceof FormStateInterface) {
$form_state = $context1;
$form_id = $context2;
// Due to a core bug that affects admin themes, we should not double
// process the "system_theme_settings" form twice in the global
// hook_form_alter() invocation.
// @see https://www.drupal.org/node/943212
if ($form_id === 'system_theme_settings') {
return;
}
// Keep track of the form identifiers.
$ids = [];
// Get the build data.
$build_info = $form_state->getBuildInfo();
// Extract the base_form_id.
$base_form_id = !empty($build_info['base_form_id']) ? $build_info['base_form_id'] : FALSE;
if ($base_form_id) {
$ids[] = $base_form_id;
}
// If there was no provided form identifier, extract it.
if (!$form_id) {
$form_id = !empty($build_info['form_id']) ? $build_info['form_id'] : Unicode::extractHook($function, 'alter', 'form');
}
if ($form_id) {
$ids[] = $form_id;
}
// Iterate over each form identifier and look for a possible plugin.
foreach ($ids as $id) {
/** @var \Drupal\bootstrap\Plugin\Form\FormInterface $form */
if ($form_manager->hasDefinition($id) && ($form = $form_manager->createInstance($id, ['theme' => $theme]))) {
$data['#submit'][] = [get_class($form), 'submitForm'];
$data['#validate'][] = [get_class($form), 'validateForm'];
$form->alterForm($data, $form_state, $form_id);
}
}
}
// Process hook alter normally.
else {
/** @var \Drupal\bootstrap\Plugin\Alter\AlterInterface $class */
if ($alter_manager->hasDefinition($hook) && ($class = $alter_manager->createInstance($hook, ['theme' => $theme]))) {
$class->alter($data, $context1, $context2);
}
}
}