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 * @internal
|
Chris@14
|
14 */
|
Chris@14
|
15 trait LayoutRebuildTrait {
|
Chris@14
|
16
|
Chris@14
|
17 /**
|
Chris@14
|
18 * The class resolver.
|
Chris@14
|
19 *
|
Chris@14
|
20 * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
|
Chris@14
|
21 */
|
Chris@14
|
22 protected $classResolver;
|
Chris@14
|
23
|
Chris@14
|
24 /**
|
Chris@14
|
25 * Rebuilds the layout.
|
Chris@14
|
26 *
|
Chris@14
|
27 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
|
Chris@14
|
28 * The section storage.
|
Chris@14
|
29 *
|
Chris@14
|
30 * @return \Drupal\Core\Ajax\AjaxResponse
|
Chris@14
|
31 * An AJAX response to either rebuild the layout and close the dialog, or
|
Chris@14
|
32 * reload the page.
|
Chris@14
|
33 */
|
Chris@14
|
34 protected function rebuildAndClose(SectionStorageInterface $section_storage) {
|
Chris@14
|
35 $response = $this->rebuildLayout($section_storage);
|
Chris@14
|
36 $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
|
Chris@14
|
37 return $response;
|
Chris@14
|
38 }
|
Chris@14
|
39
|
Chris@14
|
40 /**
|
Chris@14
|
41 * Rebuilds the layout.
|
Chris@14
|
42 *
|
Chris@14
|
43 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
|
Chris@14
|
44 * The section storage.
|
Chris@14
|
45 *
|
Chris@14
|
46 * @return \Drupal\Core\Ajax\AjaxResponse
|
Chris@14
|
47 * An AJAX response to either rebuild the layout and close the dialog, or
|
Chris@14
|
48 * reload the page.
|
Chris@14
|
49 */
|
Chris@14
|
50 protected function rebuildLayout(SectionStorageInterface $section_storage) {
|
Chris@14
|
51 $response = new AjaxResponse();
|
Chris@14
|
52 $layout_controller = $this->classResolver->getInstanceFromDefinition(LayoutBuilderController::class);
|
Chris@14
|
53 $layout = $layout_controller->layout($section_storage, TRUE);
|
Chris@14
|
54 $response->addCommand(new ReplaceCommand('#layout-builder', $layout));
|
Chris@14
|
55 return $response;
|
Chris@14
|
56 }
|
Chris@14
|
57
|
Chris@14
|
58 }
|