4 * Contains \Drupal\bootstrap\Plugin\Setting\SettingBase.
7 namespace Drupal\bootstrap\Plugin\Setting;
9 use Drupal\bootstrap\Bootstrap;
10 use Drupal\bootstrap\Plugin\PluginBase;
11 use Drupal\bootstrap\Utility\Element;
12 use Drupal\Core\Form\FormStateInterface;
16 * Base class for a setting.
18 * @ingroup plugins_setting
20 class SettingBase extends PluginBase implements SettingInterface {
25 public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
26 $this->alterFormElement(Element::create($form), $form_state);
32 public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
33 $this->getSettingElement($form, $form_state);
39 public function drupalSettings() {
46 public function getCacheTags() {
51 * Retrieves all the form properties from the setting definition.
54 * The form properties.
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]);
70 public function getDefaultValue() {
71 return isset($this->pluginDefinition['defaultValue']) ? $this->pluginDefinition['defaultValue'] : NULL;
77 * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getGroupElement
79 public function getGroup(array &$form, FormStateInterface $form_state) {
80 Bootstrap::deprecated();
81 return $this->getGroupElement(Element::create($form), $form_state);
87 public function getGroupElement(Element $form, FormStateInterface $form_state) {
88 $groups = $this->getGroups();
91 foreach ($groups as $key => $title) {
92 if (!isset($group->$key)) {
94 $group->$key = ['#type' => 'details', '#title' => $title];
97 $group->$key = ['#type' => 'container'];
99 $group = Element::create($group->$key->getArray());
101 $group->setProperty('group', 'bootstrap');
104 $group->setProperty('open', FALSE);
108 $group = Element::create($group->$key->getArray());
118 public function getGroups() {
119 return !empty($this->pluginDefinition['groups']) ? $this->pluginDefinition['groups'] : [];
125 * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getSettingElement
127 public function getElement(array &$form, FormStateInterface $form_state) {
128 Bootstrap::deprecated();
129 return $this->getSettingElement(Element::create($form), $form_state);
135 public function getOptions() {
136 return isset($this->pluginDefinition['options']) ? (array) $this->pluginDefinition['options'] : [];
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);
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);
156 // Append additional "see" link references to the description.
157 $description = (string) $group->$plugin_id->getProperty('description') ?: '';
159 foreach ($this->pluginDefinition['see'] as $url => $title) {
160 $link = Element::createStandalone([
162 '#url' => Url::fromUri($url),
165 'target' => '_blank',
168 $links[] = (string) $link->renderPlain();
170 if (!empty($links)) {
171 $description .= '<br>';
172 $description .= t('See also:');
173 $description .= ' ' . implode(', ', $links);
174 $group->$plugin_id->setProperty('description', $description);
177 return $group->$plugin_id;
182 public function getTitle() {
183 return !empty($this->pluginDefinition['title']) ? $this->pluginDefinition['title'] : NULL;
189 public static function submitForm(array &$form, FormStateInterface $form_state) {
190 static::submitFormElement(Element::create($form), $form_state);
196 public static function submitFormElement(Element $form, FormStateInterface $form_state) {}
201 public static function validateForm(array &$form, FormStateInterface $form_state) {
202 static::validateFormElement(Element::create($form), $form_state);
208 public static function validateFormElement(Element $form, FormStateInterface $form_state) {}