3 namespace Drupal\bootstrap\Plugin\Setting;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Plugin\PluginBase;
7 use Drupal\bootstrap\Utility\Element;
8 use Drupal\Core\Form\FormStateInterface;
12 * Base class for a setting.
14 * @ingroup plugins_setting
16 class SettingBase extends PluginBase implements SettingInterface {
21 public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
22 $this->alterFormElement(Element::create($form), $form_state);
28 public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
29 $this->getSettingElement($form, $form_state);
35 public function drupalSettings() {
42 public function getCacheTags() {
47 * Retrieves all the form properties from the setting definition.
50 * The form properties.
52 public function getElementProperties() {
53 $properties = $this->getPluginDefinition();
63 foreach ($properties as $name => $value) {
64 if (in_array($name, $ignore_keys)) {
65 unset($properties[$name]);
74 public function getDefaultValue() {
75 return isset($this->pluginDefinition['defaultValue']) ? $this->pluginDefinition['defaultValue'] : NULL;
81 * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getGroupElement
83 public function getGroup(array &$form, FormStateInterface $form_state) {
84 Bootstrap::deprecated();
85 return $this->getGroupElement(Element::create($form), $form_state);
91 public function getGroupElement(Element $form, FormStateInterface $form_state) {
92 $groups = $this->getGroups();
95 foreach ($groups as $key => $title) {
96 if (!isset($group->$key)) {
98 $group->$key = ['#type' => 'details', '#title' => $title];
101 $group->$key = ['#type' => 'container'];
103 $group = Element::create($group->$key->getArray());
105 $group->setProperty('group', 'bootstrap');
108 $group->setProperty('open', FALSE);
112 $group = Element::create($group->$key->getArray());
122 public function getGroups() {
123 return !empty($this->pluginDefinition['groups']) ? $this->pluginDefinition['groups'] : [];
129 * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getSettingElement
131 public function getElement(array &$form, FormStateInterface $form_state) {
132 Bootstrap::deprecated();
133 return $this->getSettingElement(Element::create($form), $form_state);
139 public function getOptions() {
140 return isset($this->pluginDefinition['options']) ? (array) $this->pluginDefinition['options'] : [];
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);
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);
160 // Append additional "see" link references to the description.
161 $description = (string) $group->$plugin_id->getProperty('description') ?: '';
163 foreach ($this->pluginDefinition['see'] as $url => $title) {
164 $link = Element::createStandalone([
166 '#url' => Url::fromUri($url),
169 'target' => '_blank',
172 $links[] = (string) $link->renderPlain();
174 if (!empty($links)) {
175 $description .= '<br>';
176 $description .= t('See also:');
177 $description .= ' ' . implode(', ', $links);
178 $group->$plugin_id->setProperty('description', $description);
182 // Hide the setting if is been deprecated.
183 if ($this instanceof DeprecatedSettingInterface) {
184 $group->$plugin_id->access(FALSE);
187 return $group->$plugin_id;
193 public function getTitle() {
194 return !empty($this->pluginDefinition['title']) ? $this->pluginDefinition['title'] : NULL;
200 public static function submitForm(array &$form, FormStateInterface $form_state) {
201 static::submitFormElement(Element::create($form), $form_state);
207 public static function submitFormElement(Element $form, FormStateInterface $form_state) {}
212 public static function validateForm(array &$form, FormStateInterface $form_state) {
213 static::validateFormElement(Element::create($form), $form_state);
219 public static function validateFormElement(Element $form, FormStateInterface $form_state) {}