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