4 * Contains \Drupal\bootstrap\Plugin\Alter\ElementInfo.
7 namespace Drupal\bootstrap\Plugin\Alter;
9 use Drupal\bootstrap\Annotation\BootstrapAlter;
10 use Drupal\bootstrap\Bootstrap;
11 use Drupal\bootstrap\Plugin\PluginBase;
12 use Drupal\bootstrap\Plugin\PrerenderManager;
13 use Drupal\bootstrap\Plugin\ProcessManager;
16 * Implements hook_element_info_alter().
18 * @ingroup plugins_alter
20 * @BootstrapAlter("element_info")
22 class ElementInfo extends PluginBase implements AlterInterface {
27 public function alter(&$types, &$context1 = NULL, &$context2 = NULL) {
28 // Sort the types for easier debugging.
29 ksort($types, SORT_NATURAL);
31 $extra_variables = Bootstrap::extraVariables();
32 $process_manager = new ProcessManager($this->theme);
33 $pre_render_manager = new PrerenderManager($this->theme);
35 foreach (array_keys($types) as $type) {
36 $element = &$types[$type];
38 // By default, the "checkboxes" and "radios" element types invoke
39 // CompositeFormElementTrait::preRenderCompositeFormElement which wraps
40 // the element in a fieldset and thus ultimately a panel. This isn't
41 // (usually) the desired effect for these elements, so to avoid rendering
42 // them as Bootstrap panels, the #bootstrap_panel should be set to FALSE
43 // by default. This allows those who wish to opt back in to do so.
44 if ($type === 'checkboxes' || $type === 'radios') {
45 $element['#bootstrap_panel'] = FALSE;
48 // Core does not actually use the "description_display" property on the
49 // "details" or "fieldset" element types because the positioning of the
50 // description is never used in core templates. However, the form builder
51 // automatically applies the value of "after", thus making it impossible
52 // to detect a valid value later in the rendering process. It looks better
53 // for the "details" and "fieldset" element types to display as "before".
54 // @see \Drupal\Core\Form\FormBuilder::doBuildForm()
55 if ($type === 'details' || $type === 'fieldset') {
56 $element['#description_display'] = 'before';
57 $element['#panel_type'] = 'default';
60 // Add extra variables as defaults to all elements.
61 foreach ($extra_variables as $key => $value) {
62 if (!isset($element["#$key"])) {
63 $element["#$key"] = $value;
67 // Only continue if the type isn't "form" (as it messes up AJAX).
68 if ($type !== 'form') {
71 // Add necessary #process callbacks.
72 $element['#process'][] = [get_class($process_manager), 'process'];
73 $definitions = $process_manager->getDefinitionsLike($regex);
74 foreach ($definitions as $definition) {
75 Bootstrap::addCallback($element['#process'], [$definition['class'], 'process'], $definition['replace'], $definition['action']);
78 // Add necessary #pre_render callbacks.
79 $element['#pre_render'][] = [get_class($pre_render_manager), 'preRender'];
80 foreach ($pre_render_manager->getDefinitionsLike($regex) as $definition) {
81 Bootstrap::addCallback($element['#pre_render'], [$definition['class'], 'preRender'], $definition['replace'], $definition['action']);