file RegionWells.php

Namespace

Drupal\bootstrap\Plugin\Setting\Components\Region
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Setting\Components\Region;
  3. use Drupal\bootstrap\Plugin\Setting\SettingBase;
  4. use Drupal\bootstrap\Utility\Element;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * The "region_wells" theme setting.
  8. *
  9. * @ingroup plugins_setting
  10. *
  11. * @BootstrapSetting(
  12. * id = "region_wells",
  13. * type = "container",
  14. * description = @Translation("Enable the <code>.well</code>, <code>.well-sm</code> or <code>.well-lg</code> classes for specified regions."),
  15. * defaultValue = {
  16. * "navigation" = "",
  17. * "navigation_collapsible" = "",
  18. * "header" = "",
  19. * "highlighted" = "",
  20. * "help" = "",
  21. * "content" = "",
  22. * "sidebar_first" = "",
  23. * "sidebar_second" = "well",
  24. * "footer" = "",
  25. * },
  26. * groups = {
  27. * "components" = @Translation("Components"),
  28. * "region_wells" = @Translation("Region Wells"),
  29. * },
  30. * see = {
  31. * "https://getbootstrap.com/docs/3.4/components/#wells" = @Translation("Bootstrap Wells"),
  32. * },
  33. * )
  34. */
  35. class RegionWells extends SettingBase {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  40. parent::alterFormElement($form, $form_state, $form_id);
  41. $group = $this->getGroupElement($form, $form_state);
  42. $setting = $this->getSettingElement($form, $form_state);
  43. // Move description.
  44. $group->setProperty('description', $setting->getProperty('description'));
  45. // Retrieve the current default values.
  46. $default_values = $setting->getProperty('default_value', $this->getDefaultValue());
  47. $wells = [
  48. '' => t('None'),
  49. 'well' => t('.well (normal)'),
  50. 'well well-sm' => t('.well-sm (small)'),
  51. 'well well-lg' => t('.well-lg (large)'),
  52. ];
  53. // Create dynamic well settings for each region.
  54. $regions = system_region_list($this->theme->getName());
  55. foreach ($regions as $name => $title) {
  56. if (in_array($name, ['page_top', 'page_bottom'])) {
  57. continue;
  58. }
  59. $setting->{'region_well-' . $name} = [
  60. '#title' => $title,
  61. '#type' => 'select',
  62. '#attributes' => [
  63. 'class' => ['input-sm'],
  64. ],
  65. '#options' => $wells,
  66. '#default_value' => isset($default_values[$name]) ? $default_values[$name] : '',
  67. ];
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public static function submitFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  74. $values = $form_state->getValues();
  75. // Extract the regions from individual dynamic settings.
  76. $regex = '/^region_well-/';
  77. $region_wells = [];
  78. foreach ($values as $key => $value) {
  79. if (!preg_match($regex, $key)) {
  80. continue;
  81. }
  82. $region_wells[preg_replace($regex, '', $key)] = $value;
  83. unset($values[$key]);
  84. }
  85. // Store the new values.
  86. $values['region_wells'] = $region_wells;
  87. $form_state->setValues($values);
  88. }
  89. }

Classes

Name Description
RegionWells The "region_wells" theme setting.