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