Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / BootstrapPanel.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Preprocess;
4
5 use Drupal\bootstrap\Utility\Element;
6 use Drupal\bootstrap\Utility\Variables;
7 use Drupal\Component\Render\MarkupInterface;
8 use Drupal\Component\Utility\Html;
9
10 /**
11  * Pre-processes variables for the "bootstrap_panel" theme hook.
12  *
13  * @ingroup plugins_preprocess
14  *
15  * @BootstrapPreprocess("bootstrap_panel")
16  */
17 class BootstrapPanel extends PreprocessBase implements PreprocessInterface {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function preprocessElement(Element $element, Variables $variables) {
23     // Assign the ID, if not already set.
24     $element->map(['id']);
25
26     // Add necessary classes.
27     $element->addClass([
28       'form-item',
29       'js-form-item',
30       'form-wrapper',
31       'js-form-wrapper',
32     ]);
33
34     $body = [];
35     $properties = ['field_prefix', 'body', 'children'];
36
37     // Only add the #value property if it's a "details" or "fieldset" element
38     // type. Some form elements may use "CompositeFormElementTrait" which
39     // will inadvertently and eventually become preprocessed here and #value
40     // may actually be the element's value instead of a renderable element.
41     if ($element->isType(['details', 'fieldset'])) {
42       $properties[] = 'value';
43     }
44
45     // Add the "#field_suffix" property.
46     $properties[] = 'field_suffix';
47
48     // Merge all possible content from the element into a single render array.
49     foreach ($properties as $property) {
50       $body[$property] = Element::create($element->getProperty($property, []))->getArray();
51     }
52     $variables['body'] = array_filter($body);
53
54     $map = [
55       'attributes' => 'attributes',
56       'body_attributes' => 'body_attributes',
57       'content_attributes' => 'body_attributes',
58       'description' => 'description',
59       'description_attributes' => 'description_attributes',
60       'description_display' => 'description_display',
61       'errors' => 'errors',
62       'footer' => 'footer',
63       'required' => 'required',
64       'panel_type' => 'panel_type',
65       'title' => 'heading',
66       'title_attributes' => 'heading_attributes',
67     ];
68
69     // Handle specific "details" elements.
70     if ($element->isType('details')) {
71       // Details are always collapsible per the HTML5 spec.
72       // @see https://www.drupal.org/node/1852020
73       $variables['collapsible'] = TRUE;
74
75       // Determine the collapsed state.
76       $variables['collapsed'] = !$element->getProperty('open', TRUE);
77
78       // Remove the unnecessary details attribute.
79       $element->removeAttribute('open');
80     }
81     // Handle specific "fieldset" elements.
82     elseif ($element->isType('fieldset')) {
83       // Override variables to mimic the default "fieldset" element info.
84       // They will be mapped below if they exist on the element.
85       unset($variables['collapsible'], $variables['collapsed']);
86       $map['collapsed'] = 'collapsed';
87       $map['collapsible'] = 'collapsible';
88     }
89
90     // Map the element properties to the variables array.
91     $variables->map($map);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function preprocessVariables(Variables $variables) {
98     // Retrieve the ID, generating one if needed.
99     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-panel')));
100     unset($variables['id']);
101
102     // Handle collapsible state.
103     if ($variables['heading'] && $variables['collapsible']) {
104       // Retrieve the body ID attribute.
105       if ($body_id = $variables->getAttribute('id', "$id--content", 'body_attributes')) {
106         // Ensure the target is set.
107         if ($variables['target'] = $variables->offsetGet('target', "#$body_id")) {
108           // Set additional necessary attributes to the heading.
109           $variables->setAttributes([
110             'aria-controls' => preg_replace('/^#/', '', $variables['target']),
111             'aria-expanded' => !$variables['collapsed'] ? 'true' : 'false',
112             'aria-pressed' => !$variables['collapsed'] ? 'true' : 'false',
113             'data-toggle' => 'collapse',
114             'role' => 'button',
115           ], 'heading_attributes');
116         }
117       }
118     }
119
120     // Ensure we render HTML from heading.
121     $heading = $variables->offsetGet('heading');
122     if ($heading && (is_string($heading) || ($heading instanceof MarkupInterface))) {
123       $variables->offsetSet('heading', ['#markup' => $heading]);
124     }
125
126     // Ensure there is a valid panel state.
127     if (!$variables->offsetGet('panel_type')) {
128       $variables->offsetSet('panel_type', 'default');
129     }
130
131     // Convert the description variable.
132     $this->preprocessDescription();
133
134     // Ensure all attributes are proper objects.
135     $this->preprocessAttributes();
136   }
137
138 }