3 namespace Drupal\layout_builder\Controller;
5 use Drupal\Core\Ajax\AjaxHelperTrait;
6 use Drupal\Core\DependencyInjection\ClassResolverInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
9 use Drupal\layout_builder\Section;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpFoundation\RedirectResponse;
15 * Defines a controller to add a new section.
19 class AddSectionController implements ContainerInjectionInterface {
22 use LayoutRebuildTrait;
25 * The layout tempstore repository.
27 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
29 protected $layoutTempstoreRepository;
32 * AddSectionController constructor.
34 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
35 * The layout tempstore repository.
36 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
39 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
40 $this->layoutTempstoreRepository = $layout_tempstore_repository;
41 $this->classResolver = $class_resolver;
47 public static function create(ContainerInterface $container) {
49 $container->get('layout_builder.tempstore_repository'),
50 $container->get('class_resolver')
55 * Adds the new section.
57 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
58 * The section storage.
60 * The delta of the section to splice.
61 * @param string $plugin_id
62 * The plugin ID of the layout to add.
64 * @return \Symfony\Component\HttpFoundation\Response
65 * The controller response.
67 public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
68 $section_storage->insertSection($delta, new Section($plugin_id));
70 $this->layoutTempstoreRepository->set($section_storage);
72 if ($this->isAjax()) {
73 return $this->rebuildAndClose($section_storage);
76 $url = $section_storage->getLayoutBuilderUrl();
77 return new RedirectResponse($url->setAbsolute()->toString());