dd2cee1611a599282dd576e6f79cb7515c83e350
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Alter / ElementInfo.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Alter\ElementInfo.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Alter;
8
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;
14
15 /**
16  * Implements hook_element_info_alter().
17  *
18  * @ingroup plugins_alter
19  *
20  * @BootstrapAlter("element_info")
21  */
22 class ElementInfo extends PluginBase implements AlterInterface {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function alter(&$types, &$context1 = NULL, &$context2 = NULL) {
28     // Sort the types for easier debugging.
29     ksort($types, SORT_NATURAL);
30
31     $extra_variables = Bootstrap::extraVariables();
32     $process_manager = new ProcessManager($this->theme);
33     $pre_render_manager = new PrerenderManager($this->theme);
34
35     foreach (array_keys($types) as $type) {
36       $element = &$types[$type];
37
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;
46       }
47
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';
58       }
59
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;
64         }
65       }
66
67       // Only continue if the type isn't "form" (as it messes up AJAX).
68       if ($type !== 'form') {
69         $regex = "/^$type/";
70
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']);
76         }
77
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']);
82         }
83       }
84     }
85   }
86
87 }