Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\layout_builder\Controller;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Core\Ajax\AjaxResponse;
|
Chris@14
|
6 use Drupal\Core\Ajax\CloseDialogCommand;
|
Chris@14
|
7 use Drupal\Core\Ajax\ReplaceCommand;
|
Chris@14
|
8 use Drupal\layout_builder\SectionStorageInterface;
|
Chris@14
|
9
|
Chris@14
|
10 /**
|
Chris@14
|
11 * Provides AJAX responses to rebuild the Layout Builder.
|
Chris@14
|
12 */
|
Chris@14
|
13 trait LayoutRebuildTrait {
|
Chris@14
|
14
|
Chris@14
|
15 /**
|
Chris@14
|
16 * Rebuilds the layout.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
|
Chris@14
|
19 * The section storage.
|
Chris@14
|
20 *
|
Chris@14
|
21 * @return \Drupal\Core\Ajax\AjaxResponse
|
Chris@14
|
22 * An AJAX response to either rebuild the layout and close the dialog, or
|
Chris@14
|
23 * reload the page.
|
Chris@14
|
24 */
|
Chris@14
|
25 protected function rebuildAndClose(SectionStorageInterface $section_storage) {
|
Chris@14
|
26 $response = $this->rebuildLayout($section_storage);
|
Chris@14
|
27 $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
|
Chris@14
|
28 return $response;
|
Chris@14
|
29 }
|
Chris@14
|
30
|
Chris@14
|
31 /**
|
Chris@14
|
32 * Rebuilds the layout.
|
Chris@14
|
33 *
|
Chris@14
|
34 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
|
Chris@14
|
35 * The section storage.
|
Chris@14
|
36 *
|
Chris@14
|
37 * @return \Drupal\Core\Ajax\AjaxResponse
|
Chris@14
|
38 * An AJAX response to either rebuild the layout and close the dialog, or
|
Chris@14
|
39 * reload the page.
|
Chris@14
|
40 */
|
Chris@14
|
41 protected function rebuildLayout(SectionStorageInterface $section_storage) {
|
Chris@14
|
42 $response = new AjaxResponse();
|
Chris@18
|
43 $layout = [
|
Chris@18
|
44 '#type' => 'layout_builder',
|
Chris@18
|
45 '#section_storage' => $section_storage,
|
Chris@18
|
46 ];
|
Chris@14
|
47 $response->addCommand(new ReplaceCommand('#layout-builder', $layout));
|
Chris@14
|
48 return $response;
|
Chris@14
|
49 }
|
Chris@14
|
50
|
Chris@14
|
51 }
|