LayoutBuilderController.php 10.8 KB
Newer Older
1
2
3
4
5
<?php

namespace Drupal\layout_builder\Controller;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6
use Drupal\Core\Messenger\MessengerInterface;
7
8
9
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
10
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
11
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
12
use Drupal\layout_builder\OverridesSectionStorageInterface;
13
use Drupal\layout_builder\Section;
14
use Drupal\layout_builder\SectionStorageInterface;
15
16
17
18
19
20
21
22
23
24
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Defines a controller to provide the Layout Builder admin UI.
 *
 * @internal
 */
class LayoutBuilderController implements ContainerInjectionInterface {

25
  use LayoutBuilderContextTrait;
26
27
28
29
30
31
32
33
34
  use StringTranslationTrait;

  /**
   * The layout tempstore repository.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   */
  protected $layoutTempstoreRepository;

35
36
37
38
39
40
41
  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

42
43
44
45
46
  /**
   * LayoutBuilderController constructor.
   *
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The layout tempstore repository.
47
48
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
49
   */
50
  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
51
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
52
    $this->messenger = $messenger;
53
54
55
56
57
58
59
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
60
61
      $container->get('layout_builder.tempstore_repository'),
      $container->get('messenger')
62
63
64
65
66
67
    );
  }

  /**
   * Provides a title callback.
   *
68
69
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
70
71
72
73
   *
   * @return string
   *   The title for the layout page.
   */
74
75
  public function title(SectionStorageInterface $section_storage) {
    return $this->t('Edit layout for %label', ['%label' => $section_storage->label()]);
76
77
78
79
80
  }

  /**
   * Renders the Layout UI.
   *
81
82
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
83
84
85
86
87
88
   * @param bool $is_rebuilding
   *   (optional) Indicates if the layout is rebuilding, defaults to FALSE.
   *
   * @return array
   *   A render array.
   */
89
90
  public function layout(SectionStorageInterface $section_storage, $is_rebuilding = FALSE) {
    $this->prepareLayout($section_storage, $is_rebuilding);
91
92
93

    $output = [];
    $count = 0;
94
95
96
    for ($i = 0; $i < $section_storage->count(); $i++) {
      $output[] = $this->buildAddSectionLink($section_storage, $count);
      $output[] = $this->buildAdministrativeSection($section_storage, $count);
97
98
      $count++;
    }
99
    $output[] = $this->buildAddSectionLink($section_storage, $count);
100
101
102
103
104
105
106
107
    $output['#attached']['library'][] = 'layout_builder/drupal.layout_builder';
    $output['#type'] = 'container';
    $output['#attributes']['id'] = 'layout-builder';
    // Mark this UI as uncacheable.
    $output['#cache']['max-age'] = 0;
    return $output;
  }

108
109
110
111
112
113
114
115
116
  /**
   * Prepares a layout for use in the UI.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   * @param bool $is_rebuilding
   *   Indicates if the layout is rebuilding.
   */
  protected function prepareLayout(SectionStorageInterface $section_storage, $is_rebuilding) {
117
    // Only add sections if the layout is new and empty.
118
119
    if (!$is_rebuilding && $section_storage->count() === 0) {
      $sections = [];
120
121
122
123
124
125
126
      // If this is an empty override, copy the sections from the corresponding
      // default.
      if ($section_storage instanceof OverridesSectionStorageInterface) {
        $sections = $section_storage->getDefaultSectionStorage()->getSections();
      }

      // For an empty layout, begin with a single section of one column.
127
128
129
130
131
132
133
134
135
136
137
      if (!$sections) {
        $sections[] = new Section('layout_onecol');
      }

      foreach ($sections as $section) {
        $section_storage->appendSection($section);
      }
      $this->layoutTempstoreRepository->set($section_storage);
    }
  }

138
139
140
  /**
   * Builds a link to add a new section at a given delta.
   *
141
142
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
143
144
145
146
147
148
   * @param int $delta
   *   The delta of the section to splice.
   *
   * @return array
   *   A render array for a link.
   */
149
150
151
  protected function buildAddSectionLink(SectionStorageInterface $section_storage, $delta) {
    $storage_type = $section_storage->getStorageType();
    $storage_id = $section_storage->getStorageId();
152
153
154
155
156
157
    return [
      'link' => [
        '#type' => 'link',
        '#title' => $this->t('Add Section'),
        '#url' => Url::fromRoute('layout_builder.choose_section',
          [
158
159
            'section_storage_type' => $storage_type,
            'section_storage' => $storage_id,
160
161
162
163
            'delta' => $delta,
          ],
          [
            'attributes' => [
164
              'class' => ['use-ajax', 'new-section__link'],
165
166
167
168
169
170
171
172
              'data-dialog-type' => 'dialog',
              'data-dialog-renderer' => 'off_canvas',
            ],
          ]
        ),
      ],
      '#type' => 'container',
      '#attributes' => [
173
        'class' => ['new-section'],
174
175
176
177
178
179
180
      ],
    ];
  }

  /**
   * Builds the render array for the layout section while editing.
   *
181
182
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
183
184
185
186
187
188
   * @param int $delta
   *   The delta of the section.
   *
   * @return array
   *   The render array for a given section.
   */
189
190
191
192
  protected function buildAdministrativeSection(SectionStorageInterface $section_storage, $delta) {
    $storage_type = $section_storage->getStorageType();
    $storage_id = $section_storage->getStorageId();
    $section = $section_storage->getSection($delta);
193

194
    $layout = $section->getLayout();
195
    $build = $section->toRenderArray($this->getAvailableContexts($section_storage), TRUE);
196
197
198
199
200
201
202
203
204
205
    $layout_definition = $layout->getPluginDefinition();

    foreach ($layout_definition->getRegions() as $region => $info) {
      if (!empty($build[$region])) {
        foreach ($build[$region] as $uuid => $block) {
          $build[$region][$uuid]['#attributes']['class'][] = 'draggable';
          $build[$region][$uuid]['#attributes']['data-layout-block-uuid'] = $uuid;
          $build[$region][$uuid]['#contextual_links'] = [
            'layout_builder_block' => [
              'route_parameters' => [
206
207
                'section_storage_type' => $storage_type,
                'section_storage' => $storage_id,
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                'delta' => $delta,
                'region' => $region,
                'uuid' => $uuid,
              ],
            ],
          ];
        }
      }

      $build[$region]['layout_builder_add_block']['link'] = [
        '#type' => 'link',
        '#title' => $this->t('Add Block'),
        '#url' => Url::fromRoute('layout_builder.choose_block',
          [
222
223
            'section_storage_type' => $storage_type,
            'section_storage' => $storage_id,
224
225
226
227
228
            'delta' => $delta,
            'region' => $region,
          ],
          [
            'attributes' => [
229
              'class' => ['use-ajax', 'new-block__link'],
230
231
232
233
234
235
236
              'data-dialog-type' => 'dialog',
              'data-dialog-renderer' => 'off_canvas',
            ],
          ]
        ),
      ];
      $build[$region]['layout_builder_add_block']['#type'] = 'container';
237
      $build[$region]['layout_builder_add_block']['#attributes'] = ['class' => ['new-block']];
238
      $build[$region]['layout_builder_add_block']['#weight'] = 1000;
239
240
241
242
243
      $build[$region]['#attributes']['data-region'] = $region;
      $build[$region]['#attributes']['class'][] = 'layout-builder--layout__region';
    }

    $build['#attributes']['data-layout-update-url'] = Url::fromRoute('layout_builder.move_block', [
244
245
      'section_storage_type' => $storage_type,
      'section_storage' => $storage_id,
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    ])->toString();
    $build['#attributes']['data-layout-delta'] = $delta;
    $build['#attributes']['class'][] = 'layout-builder--layout';

    return [
      '#type' => 'container',
      '#attributes' => [
        'class' => ['layout-section'],
      ],
      'configure' => [
        '#type' => 'link',
        '#title' => $this->t('Configure section'),
        '#access' => $layout instanceof PluginFormInterface,
        '#url' => Url::fromRoute('layout_builder.configure_section', [
260
261
          'section_storage_type' => $storage_type,
          'section_storage' => $storage_id,
262
263
264
265
266
267
268
269
270
271
272
273
          'delta' => $delta,
        ]),
        '#attributes' => [
          'class' => ['use-ajax', 'configure-section'],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ],
      'remove' => [
        '#type' => 'link',
        '#title' => $this->t('Remove section'),
        '#url' => Url::fromRoute('layout_builder.remove_section', [
274
275
          'section_storage_type' => $storage_type,
          'section_storage' => $storage_id,
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
          'delta' => $delta,
        ]),
        '#attributes' => [
          'class' => ['use-ajax', 'remove-section'],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ],
      'layout-section' => $build,
    ];
  }

  /**
   * Saves the layout.
   *
291
292
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
293
294
295
296
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response.
   */
297
298
299
  public function saveLayout(SectionStorageInterface $section_storage) {
    $section_storage->save();
    $this->layoutTempstoreRepository->delete($section_storage);
300
301
302
303
304
305
306
307

    if ($section_storage instanceof OverridesSectionStorageInterface) {
      $this->messenger->addMessage($this->t('The layout override has been saved.'));
    }
    else {
      $this->messenger->addMessage($this->t('The layout has been saved.'));
    }

308
    return new RedirectResponse($section_storage->getRedirectUrl()->setAbsolute()->toString());
309
310
311
312
313
  }

  /**
   * Cancels the layout.
   *
314
315
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
316
317
318
319
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response.
   */
320
321
  public function cancelLayout(SectionStorageInterface $section_storage) {
    $this->layoutTempstoreRepository->delete($section_storage);
322
323
324

    $this->messenger->addMessage($this->t('The changes to the layout have been discarded.'));

325
    return new RedirectResponse($section_storage->getRedirectUrl()->setAbsolute()->toString());
326
327
328
  }

}