3 namespace Drupal\layout_builder\Controller;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\CloseDialogCommand;
7 use Drupal\Core\Ajax\ReplaceCommand;
8 use Drupal\layout_builder\SectionStorageInterface;
11 * Provides AJAX responses to rebuild the Layout Builder.
15 trait LayoutRebuildTrait {
20 * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
22 protected $classResolver;
25 * Rebuilds the layout.
27 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
28 * The section storage.
30 * @return \Drupal\Core\Ajax\AjaxResponse
31 * An AJAX response to either rebuild the layout and close the dialog, or
34 protected function rebuildAndClose(SectionStorageInterface $section_storage) {
35 $response = $this->rebuildLayout($section_storage);
36 $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
41 * Rebuilds the layout.
43 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
44 * The section storage.
46 * @return \Drupal\Core\Ajax\AjaxResponse
47 * An AJAX response to either rebuild the layout and close the dialog, or
50 protected function rebuildLayout(SectionStorageInterface $section_storage) {
51 $response = new AjaxResponse();
52 $layout_controller = $this->classResolver->getInstanceFromDefinition(LayoutBuilderController::class);
53 $layout = $layout_controller->layout($section_storage, TRUE);
54 $response->addCommand(new ReplaceCommand('#layout-builder', $layout));