Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / bootstrap_layouts / src / Plugin / Layout / BootstrapLayoutsBase.php
1 <?php
2
3 namespace Drupal\bootstrap_layouts\Plugin\Layout;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Component\Utility\Xss;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\layout_plugin\Plugin\Layout\LayoutBase;
10
11 /**
12  * Layout class for all bootstrap layouts.
13  */
14 class BootstrapLayoutsBase extends LayoutBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
20     parent::__construct($configuration, $plugin_id, $plugin_definition);
21   }
22
23   /**
24    * Provides a default region definition.
25    *
26    * @return array
27    *   Default region array.
28    */
29   protected function getRegionDefaults() {
30     return [
31       'wrapper' => 'div',
32       'classes' => [],
33       'attributes' => '',
34       'add_region_classes' => TRUE,
35     ];
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function defaultConfiguration() {
42     $configuration = parent::defaultConfiguration();
43     $configuration += [
44       'layout' => [
45         'wrapper' => 'div',
46         'classes' => ['row'],
47         'attributes' => '',
48         'add_layout_class' => TRUE,
49       ],
50       'regions' => [],
51     ];
52     foreach ($this->getRegionDefinitions() as $region => $info) {
53       $region_configuration = [];
54       foreach (['wrapper', 'classes', 'attributes'] as $key) {
55         if (isset($info[$key])) {
56           $region_configuration[$key] = $info[$key];
57         }
58       }
59       $configuration['regions'][$region] = $region_configuration + $this->getRegionDefaults();
60     }
61     return $configuration;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getConfiguration() {
68     $configuration = $this->defaultConfiguration();
69
70     // Can't use parent::getConfiguration() here because array_merge() only
71     // merges the top levels. Nor can NestedArray::mergeDeep be used since it
72     // will add multiple classes (from default + config). Instead, the two
73     // top levels "layout" and "regions" must be merged using array_merge().
74     if (isset($this->configuration['layout'])) {
75       $configuration['layout'] = array_merge($configuration['layout'], $this->configuration['layout']);
76     }
77     if (isset($this->configuration['regions'])) {
78       $configuration['regions'] = array_merge($configuration['regions'], $this->configuration['regions']);
79     }
80
81     // Remove any region configuration that doesn't apply to current layout.
82     $regions = $this->getRegionNames();
83     foreach (array_keys($configuration['regions']) as $region) {
84       if (!isset($regions[$region])) {
85         unset($configuration['regions'][$region]);
86       }
87     }
88
89     return $configuration;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
96     $form = parent::buildConfigurationForm($form, $form_state);
97     $configuration = $this->getConfiguration();
98
99     /** @var \Drupal\bootstrap_layouts\BootstrapLayoutsManager $bootstrap_layouts_manager */
100     $manager = \Drupal::getContainer()->get('plugin.manager.bootstrap_layouts');
101     $classes = $manager->getClassOptions();
102
103     $tokens = FALSE;
104     if (\Drupal::moduleHandler()->moduleExists('token')) {
105       $tokens = [
106         '#title' => $this->t('Tokens'),
107         '#type' => 'container',
108       ];
109       $tokens['help'] = [
110         '#theme' => 'token_tree_link',
111         '#token_types' => 'all',
112         '#global_types' => FALSE,
113         '#dialog' => TRUE,
114       ];
115     }
116
117     // Add wrappers.
118     $wrapper_options = [
119       'div' => 'Div',
120       'span' => 'Span',
121       'section' => 'Section',
122       'article' => 'Article',
123       'header' => 'Header',
124       'footer' => 'Footer',
125       'aside' => 'Aside',
126       'figure' => 'Figure',
127     ];
128
129     $form['layout'] = [
130       '#type' => 'container',
131       '#tree' => TRUE,
132     ];
133
134     $form['layout']['wrapper'] = [
135       '#type' => 'select',
136       '#title' => $this->t('Wrapper'),
137       '#options' => $wrapper_options,
138       '#default_value' => $form_state->getValue(['layout', 'wrapper'], $configuration['layout']['wrapper']),
139     ];
140
141     $form['layout']['classes'] = [
142       '#type' => 'select',
143       '#title' => $this->t('Classes'),
144       '#options' => $classes,
145       '#default_value' => $form_state->getValue(['layout', 'classes'], $configuration['layout']['classes']) ?: [],
146       '#multiple' => TRUE,
147     ];
148
149     $form['layout']['add_layout_class'] = [
150       '#type' => 'checkbox',
151       '#title' => $this->t('Add layout specific class: <code>@class</code>', ['@class' => Html::cleanCssIdentifier($this->getPluginId())]),
152       '#default_value' => (int) $form_state->getValue(['layout', 'add_layout_class'], $configuration['layout']['add_layout_class']),
153     ];
154
155     $form['layout']['attributes'] = [
156       '#type' => 'textfield',
157       '#title' => $this->t('Additional attributes'),
158       '#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
159       '#default_value' => $form_state->getValue(['layout', 'attributes'], $configuration['layout']['attributes']),
160     ];
161
162     if ($tokens) {
163       $form['layout']['tokens'] = $tokens;
164     }
165
166     // Add each region's settings.
167     foreach ($this->getRegionNames() as $region => $region_label) {
168       $default_values = NestedArray::mergeDeep(
169         $this->getRegionDefaults(),
170         isset($configuration['regions'][$region]) ? $configuration['regions'][$region] : [],
171         $form_state->getValue(['regions', $region], [])
172       );
173
174       $form[$region] = [
175         '#group' => 'additional_settings',
176         '#type' => 'details',
177         '#open' => TRUE,
178         '#title' => $this->t('Region: @region', ['@region' => $region_label]),
179         '#weight' => 20,
180       ];
181
182       $form[$region]['wrapper'] = [
183         '#type' => 'select',
184         '#title' => $this->t('Wrapper'),
185         '#options' => $wrapper_options,
186         '#default_value' => $default_values['wrapper'],
187       ];
188
189       $form[$region]['classes'] = [
190         '#type' => 'select',
191         '#title' => $this->t('Classes'),
192         '#options' => $classes,
193         '#default_value' => $default_values['classes'],
194         '#multiple' => TRUE,
195       ];
196
197       $form[$region]['add_region_classes'] = [
198         '#type' => 'checkbox',
199         '#title' => $this->t('Add region specific classes: <code>bs-region</code> and <code>bs-region--@region</code>', ['@region' => $region]),
200         '#default_value' => (int) $default_values['add_region_classes'],
201       ];
202
203       $form[$region]['attributes'] = [
204         '#type' => 'textfield',
205         '#title' => $this->t('Additional attributes'),
206         '#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
207         '#default_value' => $default_values['attributes'],
208       ];
209
210       if ($tokens) {
211         $form[$region]['tokens'] = $tokens;
212       }
213     }
214
215     return $form;
216   }
217
218   /**
219    * {@inheritdoc}
220    */
221   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
222     $defaults = $this->getRegionDefaults();
223     if ($layout = $form_state->getValue('layout', $defaults)) {
224       // Apply Xss::filter to attributes.
225       $layout['attributes'] = Xss::filter($layout['attributes']);
226       $this->configuration['layout'] = $layout;
227     }
228
229
230     $regions = [];
231     foreach (array_keys($this->getRegionNames()) as $name) {
232       if ($region = $form_state->getValue($name, $defaults)) {
233         // Apply Xss::filter to attributes.
234         $region['attributes'] = Xss::filter($region['attributes']);
235         $regions[$name] = $region;
236       }
237     }
238     $this->configuration['regions'] = $regions;
239   }
240
241 }