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