1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
<?php
namespace Drupal\layout_builder\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Render\Attribute\RenderElement;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElementBase;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Security\Attribute\TrustedCallback;
use Drupal\Core\Url;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Event\PrepareLayoutEvent;
use Drupal\layout_builder\LayoutBuilderEvents;
use Drupal\layout_builder\LayoutBuilderHighlightTrait;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Defines a render element for building the Layout Builder UI.
*
* @internal
* Plugin classes are internal.
*/
#[RenderElement('layout_builder')]
class LayoutBuilder extends RenderElementBase implements ContainerFactoryPluginInterface {
use AjaxHelperTrait;
use LayoutBuilderContextTrait;
use LayoutBuilderHighlightTrait;
/**
* The event dispatcher.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a new LayoutBuilder.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
* @param \Drupal\Core\Render\ElementInfoManagerInterface|null $elementInfoManager
* The element info manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, ?ElementInfoManagerInterface $elementInfoManager = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $elementInfoManager);
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('event_dispatcher'),
$container->get('plugin.manager.element_info')
);
}
/**
* {@inheritdoc}
*/
public function getInfo() {
return [
'#section_storage' => NULL,
'#pre_render' => [
[$this, 'preRender'],
],
'#process' => [
[static::class, 'layoutBuilderElementGetKeys'],
],
];
}
/**
* Form element #process callback.
*
* Save the layout builder element array parents as a property on the top form
* element, so that they can be used to access the element within the form
* render array later.
*
* @param array $element
* The render array for the layout builder element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
* @param array $form
* The render array for the complete form.
*
* @return array
* The layout builder element render array after processing.
*
* @see \Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController
*/
public static function layoutBuilderElementGetKeys(array $element, FormStateInterface $form_state, array &$form): array {
$form['#layout_builder_element_keys'] = $element['#array_parents'];
$form['#pre_render'][] = [static::class, 'renderLayoutBuilderAfterForm'];
$form['#post_render'][] = [static::class, 'addRenderedLayoutBuilder'];
return $element;
}
/**
* Render API #pre_render callback for form containing layout builder element.
*
* Because the layout builder element can contain components with forms, it
* needs to exist outside forms within the DOM, to avoid nested form tags.
* The layout builder element is rendered to markup here and saved, and later
* the saved markup will be appended after the form markup.
*
* @param array $form
* The rendered form.
*
* @return array
* Renders the layout builder element, if it exists, and adds it to the
* form.
*
* @see ::addRenderedLayoutBuilder()
*/
#[TrustedCallback]
public static function renderLayoutBuilderAfterForm(array $form): array {
if (isset($form['#layout_builder_element_keys'])) {
$layout_builder_element = &NestedArray::getValue($form, $form['#layout_builder_element_keys']);
// Save the rendered layout builder HTML to a non-rendering child key.
// Since this method is a pre_render callback, it is assumed that it is
// called while rendering with an active render context, so that the
// cache metadata and attachments bubble correctly.
$form['#layout_builder_markup'] = \Drupal::service('renderer')->render($layout_builder_element);
// Remove the layout builder child element within form array.
$layout_builder_element = [];
}
return $form;
}
/**
* Render API #post_render callback that adds layout builder markup to form.
*
* @param string $html
* The rendered form.
* @param array $form
* The form render array.
*
* @return string
* The render string with any layout builder markup added.
*/
#[TrustedCallback]
public static function addRenderedLayoutBuilder(string $html, array $form): string {
if (isset($form['#layout_builder_markup'])) {
$html .= $form['#layout_builder_markup'];
}
return $html;
}
/**
* Pre-render callback: Renders the Layout Builder UI.
*/
public function preRender($element) {
if ($element['#section_storage'] instanceof SectionStorageInterface) {
$element['layout_builder'] = $this->layout($element['#section_storage']);
}
return $element;
}
/**
* Renders the Layout UI.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
*
* @return array
* A render array.
*/
protected function layout(SectionStorageInterface $section_storage) {
$this->prepareLayout($section_storage);
$output = [];
if ($this->isAjax()) {
$output['status_messages'] = [
'#type' => 'status_messages',
];
}
$count = 0;
for ($i = 0; $i < $section_storage->count(); $i++) {
$output[] = $this->buildAddSectionLink($section_storage, $count);
$output[] = $this->buildAdministrativeSection($section_storage, $count);
$count++;
}
$output[] = $this->buildAddSectionLink($section_storage, $count);
$output['#attached']['library'][] = 'layout_builder/drupal.layout_builder';
// As the Layout Builder UI is typically displayed using the frontend theme,
// it is not marked as an administrative page at the route level even though
// it performs an administrative task. Mark this as an administrative page
// for JavaScript.
$output['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
$output['#type'] = 'container';
$output['#attributes']['id'] = 'layout-builder';
$output['#attributes']['class'][] = 'layout-builder';
$output['#attributes']['class'][] = Html::getClass('layout-builder--' . $section_storage->getPluginId());
// Mark this UI as uncacheable.
$output['#cache']['max-age'] = 0;
return $output;
}
/**
* Prepares a layout for use in the UI.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
*/
protected function prepareLayout(SectionStorageInterface $section_storage) {
$event = new PrepareLayoutEvent($section_storage);
$this->eventDispatcher->dispatch($event, LayoutBuilderEvents::PREPARE_LAYOUT);
}
/**
* Builds a link to add a new section at a given delta.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
* @param int $delta
* The delta of the section to splice.
*
* @return array
* A render array for a link.
*/
protected function buildAddSectionLink(SectionStorageInterface $section_storage, $delta) {
$storage_type = $section_storage->getStorageType();
$storage_id = $section_storage->getStorageId();
// If the delta and the count are the same, it is either the end of the
// layout or an empty layout.
if ($delta === count($section_storage)) {
if ($delta === 0) {
$title = $this->t('Add section');
}
else {
$title = $this->t('Add section <span class="visually-hidden">at end of layout</span>');
}
}
// If the delta and the count are different, it is either the beginning of
// the layout or in between two sections.
else {
if ($delta === 0) {
$title = $this->t('Add section <span class="visually-hidden">at start of layout</span>');
}
else {
$title = $this->t('Add section <span class="visually-hidden">between @first and @second</span>', ['@first' => $delta, '@second' => $delta + 1]);
}
}
return [
'link' => [
'#type' => 'link',
'#title' => $title,
'#url' => Url::fromRoute('layout_builder.choose_section',
[
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
],
[
'attributes' => [
'class' => [
'use-ajax',
'layout-builder__link',
'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
]
),
],
'#type' => 'container',
'#attributes' => [
'class' => ['layout-builder__add-section'],
'data-layout-builder-highlight-id' => $this->sectionAddHighlightId($delta),
],
];
}
/**
* Builds the render array for the layout section while editing.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
* @param int $delta
* The delta of the section.
*
* @return array
* The render array for a given section.
*/
protected function buildAdministrativeSection(SectionStorageInterface $section_storage, $delta) {
$storage_type = $section_storage->getStorageType();
$storage_id = $section_storage->getStorageId();
$section = $section_storage->getSection($delta);
$layout = $section->getLayout($this->getPopulatedContexts($section_storage));
$layout_settings = $section->getLayoutSettings();
$section_label = !empty($layout_settings['label']) ? $layout_settings['label'] : $this->t('Section @section', ['@section' => $delta + 1]);
$build = $section->toRenderArray($this->getPopulatedContexts($section_storage), TRUE);
$layout_definition = $layout->getPluginDefinition();
$region_labels = $layout_definition->getRegionLabels();
foreach ($layout_definition->getRegions() as $region => $info) {
if (!empty($build[$region])) {
foreach (Element::children($build[$region]) as $uuid) {
$build[$region][$uuid]['#attributes']['class'][] = 'js-layout-builder-block';
$build[$region][$uuid]['#attributes']['class'][] = 'layout-builder-block';
$build[$region][$uuid]['#attributes']['data-layout-block-uuid'] = $uuid;
$build[$region][$uuid]['#attributes']['data-layout-builder-highlight-id'] = $this->blockUpdateHighlightId($uuid);
$build[$region][$uuid]['#contextual_links'] = [
'layout_builder_block' => [
'route_parameters' => [
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
'region' => $region,
'uuid' => $uuid,
],
// Add metadata about the current operations available in
// contextual links. This will invalidate the client-side cache of
// links that were cached before the 'move' link was added.
// @see layout_builder.links.contextual.yml
'metadata' => [
'operations' => 'move:update:remove',
],
],
];
}
}
$build[$region]['layout_builder_add_block']['link'] = [
'#type' => 'link',
// Add one to the current delta since it is zero-indexed.
'#title' => $this->t('Add block <span class="visually-hidden">in @section, @region region</span>', ['@section' => $section_label, '@region' => $region_labels[$region]]),
'#url' => Url::fromRoute('layout_builder.choose_block',
[
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
'region' => $region,
],
[
'attributes' => [
'class' => [
'use-ajax',
'layout-builder__link',
'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
]
),
];
$build[$region]['layout_builder_add_block']['#type'] = 'container';
$build[$region]['layout_builder_add_block']['#attributes'] = [
'class' => ['layout-builder__add-block'],
'data-layout-builder-highlight-id' => $this->blockAddHighlightId($delta, $region),
];
$build[$region]['layout_builder_add_block']['#weight'] = 1000;
$build[$region]['#attributes']['data-region'] = $region;
$build[$region]['#attributes']['class'][] = 'layout-builder__region';
$build[$region]['#attributes']['class'][] = 'js-layout-builder-region';
$build[$region]['#attributes']['role'] = 'group';
$build[$region]['#attributes']['aria-label'] = $this->t('@region region in @section', [
'@region' => $info['label'],
'@section' => $section_label,
]);
// Get weights of all children for use by the region label.
$weights = array_map(function ($a) {
return $a['#weight'] ?? 0;
}, $build[$region]);
// The region label is made visible when the move block dialog is open.
$build[$region]['region_label'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['layout__region-info', 'layout-builder__region-label'],
// A more detailed version of this information is already read by
// screen readers, so this label can be hidden from them.
'aria-hidden' => TRUE,
],
'#markup' => $this->t('Region: @region', ['@region' => $info['label']]),
// Ensures the region label is displayed first.
'#weight' => min($weights) - 1,
];
}
$build['#attributes']['data-layout-update-url'] = Url::fromRoute('layout_builder.move_block', [
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
])->toString();
$build['#attributes']['data-layout-delta'] = $delta;
$build['#attributes']['class'][] = 'layout-builder__layout';
$build['#attributes']['data-layout-builder-highlight-id'] = $this->sectionUpdateHighlightId($delta);
return [
'#type' => 'container',
'#attributes' => [
'class' => ['layout-builder__section'],
'role' => 'group',
'aria-label' => $section_label,
],
'remove' => [
'#type' => 'link',
'#title' => $this->t('Remove @section', ['@section' => $section_label]),
'#url' => Url::fromRoute('layout_builder.remove_section', [
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
]),
'#attributes' => [
'class' => [
'use-ajax',
'layout-builder__link',
'layout-builder__link--remove',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
],
// The section label is added to sections without a "Configure section"
// link, and is only visible when the move block dialog is open.
'section_label' => [
'#markup' => $this->t('<span class="layout-builder__section-label" aria-hidden="true">@section</span>', ['@section' => $section_label]),
'#access' => !$layout instanceof PluginFormInterface,
],
'configure' => [
'#type' => 'link',
'#title' => $this->t('Configure @section', ['@section' => $section_label]),
'#access' => $layout instanceof PluginFormInterface,
'#url' => Url::fromRoute('layout_builder.configure_section', [
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
]),
'#attributes' => [
'class' => [
'use-ajax',
'layout-builder__link',
'layout-builder__link--configure',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
],
'layout-builder__section' => $build,
];
}
}
|