d8f85c0382f131ae4a192b5430fffcda6cae3967
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Setting / SettingBase.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Setting;
4
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Plugin\PluginBase;
7 use Drupal\bootstrap\Utility\Element;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10
11 /**
12  * Base class for a setting.
13  *
14  * @ingroup plugins_setting
15  */
16 class SettingBase extends PluginBase implements SettingInterface {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
22     $this->alterFormElement(Element::create($form), $form_state);
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
29     $this->getSettingElement($form, $form_state);
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function drupalSettings() {
36     return FALSE;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getCacheTags() {
43     return ['rendered'];
44   }
45
46   /**
47    * Retrieves all the form properties from the setting definition.
48    *
49    * @return array
50    *   The form properties.
51    */
52   public function getElementProperties() {
53     $properties = $this->getPluginDefinition();
54     $ignore_keys = [
55       'class',
56       'defaultValue',
57       'definition',
58       'groups',
59       'id',
60       'provider',
61       'see',
62     ];
63     foreach ($properties as $name => $value) {
64       if (in_array($name, $ignore_keys)) {
65         unset($properties[$name]);
66       }
67     }
68     return $properties;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getDefaultValue() {
75     return isset($this->pluginDefinition['defaultValue']) ? $this->pluginDefinition['defaultValue'] : NULL;
76   }
77
78   /**
79    * {@inheritdoc}
80    *
81    * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getGroupElement
82    */
83   public function getGroup(array &$form, FormStateInterface $form_state) {
84     Bootstrap::deprecated();
85     return $this->getGroupElement(Element::create($form), $form_state);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getGroupElement(Element $form, FormStateInterface $form_state) {
92     $groups = $this->getGroups();
93     $group = $form;
94     $first = TRUE;
95     foreach ($groups as $key => $title) {
96       if (!isset($group->$key)) {
97         if ($title) {
98           $group->$key = ['#type' => 'details', '#title' => $title];
99         }
100         else {
101           $group->$key = ['#type' => 'container'];
102         }
103         $group = Element::create($group->$key->getArray());
104         if ($first) {
105           $group->setProperty('group', 'bootstrap');
106         }
107         else {
108           $group->setProperty('open', FALSE);
109         }
110       }
111       else {
112         $group = Element::create($group->$key->getArray());
113       }
114       $first = FALSE;
115     }
116     return $group;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function getGroups() {
123     return !empty($this->pluginDefinition['groups']) ? $this->pluginDefinition['groups'] : [];
124   }
125
126   /**
127    * {@inheritdoc}
128    *
129    * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getSettingElement
130    */
131   public function getElement(array &$form, FormStateInterface $form_state) {
132     Bootstrap::deprecated();
133     return $this->getSettingElement(Element::create($form), $form_state);
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function getOptions() {
140     return isset($this->pluginDefinition['options']) ? (array) $this->pluginDefinition['options'] : [];
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function getSettingElement(Element $form, FormStateInterface $form_state) {
147     // Construct the group elements.
148     $group = $this->getGroupElement($form, $form_state);
149     $plugin_id = $this->getPluginId();
150     if (!isset($group->$plugin_id)) {
151       // Set properties from the plugin definition.
152       foreach ($this->getElementProperties() as $name => $value) {
153         $group->$plugin_id->setProperty($name, $value);
154       }
155
156       // Set default value from the stored form state value or theme setting.
157       $default_value = $form_state->getValue($plugin_id, $this->theme->getSetting($plugin_id));
158       $group->$plugin_id->setProperty('default_value', $default_value);
159
160       // Append additional "see" link references to the description.
161       $description = (string) $group->$plugin_id->getProperty('description') ?: '';
162       $links = [];
163       foreach ($this->pluginDefinition['see'] as $url => $title) {
164         $link = Element::createStandalone([
165           '#type' => 'link',
166           '#url' => Url::fromUri($url),
167           '#title' => $title,
168           '#attributes' => [
169             'target' => '_blank',
170           ],
171         ]);
172         $links[] = (string) $link->renderPlain();
173       }
174       if (!empty($links)) {
175         $description .= '<br>';
176         $description .= t('See also:');
177         $description .= ' ' . implode(', ', $links);
178         $group->$plugin_id->setProperty('description', $description);
179       }
180     }
181     return $group->$plugin_id;
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function getTitle() {
188     return !empty($this->pluginDefinition['title']) ? $this->pluginDefinition['title'] : NULL;
189   }
190
191   /**
192    * {@inheritdoc}
193    */
194   public static function submitForm(array &$form, FormStateInterface $form_state) {
195     static::submitFormElement(Element::create($form), $form_state);
196   }
197
198   /**
199    * {@inheritdoc}
200    */
201   public static function submitFormElement(Element $form, FormStateInterface $form_state) {}
202
203   /**
204    * {@inheritdoc}
205    */
206   public static function validateForm(array &$form, FormStateInterface $form_state) {
207     static::validateFormElement(Element::create($form), $form_state);
208   }
209
210   /**
211    * {@inheritdoc}
212    */
213   public static function validateFormElement(Element $form, FormStateInterface $form_state) {}
214
215 }