file SettingBase.php

Namespace

Drupal\bootstrap\Plugin\Setting
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Setting;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Plugin\PluginBase;
  5. use Drupal\bootstrap\Utility\Element;
  6. use Drupal\Core\Access\AccessResultAllowed;
  7. use Drupal\Core\Access\AccessResultForbidden;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Url;
  10. /**
  11. * Base class for a setting.
  12. *
  13. * @ingroup plugins_setting
  14. */
  15. class SettingBase extends PluginBase implements SettingInterface {
  16. public static $autoUserInterface = TRUE;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function access() {
  21. // Hide the setting if is been deprecated.
  22. if ($this instanceof DeprecatedSettingInterface) {
  23. return AccessResultForbidden::forbidden();
  24. }
  25. return AccessResultAllowed::allowed();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
  31. $this->alterFormElement(Element::create($form, $form_state), $form_state);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  37. $this->getSettingElement($form, $form_state);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function autoCreateFormElement() {
  43. return !($this instanceof DeprecatedSettingInterface);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function drupalSettings() {
  49. return FALSE;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getCacheTags() {
  55. return ['rendered'];
  56. }
  57. /**
  58. * Retrieves all the form properties from the setting definition.
  59. *
  60. * @return array
  61. * The form properties.
  62. */
  63. public function getElementProperties() {
  64. $properties = $this->getPluginDefinition();
  65. $ignore_keys = [
  66. 'class',
  67. 'defaultValue',
  68. 'definition',
  69. 'groups',
  70. 'id',
  71. 'provider',
  72. 'see',
  73. ];
  74. foreach ($properties as $name => $value) {
  75. if (in_array($name, $ignore_keys)) {
  76. unset($properties[$name]);
  77. }
  78. }
  79. return $properties;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getDefaultValue() {
  85. return isset($this->pluginDefinition['defaultValue']) ? $this->pluginDefinition['defaultValue'] : NULL;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getDescription() {
  91. return isset($this->pluginDefinition['description']) ? $this->pluginDefinition['description'] : NULL;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. *
  96. * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getGroupElement
  97. */
  98. public function getGroup(array &$form, FormStateInterface $form_state) {
  99. Bootstrap::deprecated();
  100. return $this->getGroupElement(Element::create($form, $form_state), $form_state);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getGroupElement(Element $form, FormStateInterface $form_state) {
  106. $groups = $this->getGroups();
  107. $group = $form;
  108. $first = TRUE;
  109. foreach ($groups as $key => $title) {
  110. if (!isset($group->$key)) {
  111. if ($title) {
  112. $group->$key = ['#type' => 'details', '#title' => $title];
  113. }
  114. else {
  115. $group->$key = ['#type' => 'container'];
  116. }
  117. $group = Element::create($group->$key->getArray(), $form_state);
  118. if ($first) {
  119. $group->setProperty('group', 'bootstrap');
  120. }
  121. else {
  122. $group->setProperty('open', FALSE);
  123. }
  124. }
  125. else {
  126. $group = Element::create($group->$key->getArray(), $form_state);
  127. }
  128. $first = FALSE;
  129. }
  130. return $group;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getGroups() {
  136. return !empty($this->pluginDefinition['groups']) ? $this->pluginDefinition['groups'] : [];
  137. }
  138. /**
  139. * {@inheritdoc}
  140. *
  141. * @deprecated Will be removed in a future release. Use \Drupal\bootstrap\Plugin\Setting\SettingInterface::getSettingElement
  142. */
  143. public function getElement(array &$form, FormStateInterface $form_state) {
  144. Bootstrap::deprecated();
  145. return $this->getSettingElement(Element::create($form, $form_state), $form_state);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getOptions() {
  151. return isset($this->pluginDefinition['options']) ? (array) $this->pluginDefinition['options'] : [];
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getSettingElement(Element $form, FormStateInterface $form_state) {
  157. // Construct the group elements.
  158. $group = $this->getGroupElement($form, $form_state);
  159. $plugin_id = $this->getPluginId();
  160. if (!isset($group->$plugin_id)) {
  161. // Set properties from the plugin definition.
  162. foreach ($this->getElementProperties() as $name => $value) {
  163. $group->$plugin_id->setProperty($name, $value);
  164. }
  165. // Get the default value.
  166. $default_value = $this->getSettingValue($form_state);
  167. // Convert value from an array into a newline separated value.
  168. // @todo Remove once settings have proper config schemas in place.
  169. // @see https://www.drupal.org/project/bootstrap/issues/2883714
  170. if ($group->$plugin_id->getProperty('type') === 'textarea' && is_array($default_value)) {
  171. $default_value = implode("\n", $default_value);
  172. }
  173. // Set default value from the stored form state value or theme setting.
  174. $group->$plugin_id->setProperty('default_value', $default_value);
  175. // Append additional "see" link references to the description.
  176. $description = (string) $group->$plugin_id->getProperty('description') ?: '';
  177. $links = [];
  178. foreach ($this->pluginDefinition['see'] as $url => $title) {
  179. $link = Element::createStandalone([
  180. '#type' => 'link',
  181. '#url' => Url::fromUri($url),
  182. '#title' => $title,
  183. '#attributes' => [
  184. 'target' => '_blank',
  185. ],
  186. ], $form_state);
  187. $links[] = (string) $link->renderPlain();
  188. }
  189. if (!empty($links)) {
  190. $description .= '<br>';
  191. $description .= t('See also:');
  192. $description .= ' ' . implode(', ', $links);
  193. $group->$plugin_id->setProperty('description', $description);
  194. }
  195. }
  196. // Set accessibility.
  197. $group->$plugin_id->access($this->access());
  198. return $group->$plugin_id;
  199. }
  200. /**
  201. * Retrieves the setting value used to populate the form.
  202. *
  203. * @param \Drupal\Core\Form\FormStateInterface $form_state
  204. * The current form state.
  205. *
  206. * @return mixed
  207. * The setting value.
  208. */
  209. protected function getSettingValue(FormStateInterface $form_state) {
  210. $plugin_id = $this->getPluginId();
  211. return $form_state->getValue($plugin_id, $this->theme->getSetting($plugin_id));
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function getTitle() {
  217. return !empty($this->pluginDefinition['title']) ? $this->pluginDefinition['title'] : NULL;
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function processDeprecatedValues(array $values, array $deprecated) {
  223. // Most deprecated settings will be a 1:1 map. Anything more complex than
  224. // this should be handled by the newer replacement setting itself.
  225. return !empty($values) ? reset($values) : NULL;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public static function submitForm(array &$form, FormStateInterface $form_state) {
  231. static::submitFormElement(Element::create($form, $form_state), $form_state);
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public static function submitFormElement(Element $form, FormStateInterface $form_state) {}
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public static function validateForm(array &$form, FormStateInterface $form_state) {
  241. static::validateFormElement(Element::create($form, $form_state), $form_state);
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public static function validateFormElement(Element $form, FormStateInterface $form_state) {}
  247. }

Classes

Name Description
SettingBase Base class for a setting.