3 namespace Drupal\layout_builder\Controller;
5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
8 use Drupal\layout_builder\Section;
9 use Drupal\layout_builder\SectionStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
14 * Defines a controller to add a new section.
18 class AddSectionController implements ContainerInjectionInterface {
21 use LayoutRebuildTrait;
24 * The layout tempstore repository.
26 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
28 protected $layoutTempstoreRepository;
31 * AddSectionController constructor.
33 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
34 * The layout tempstore repository.
35 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
38 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
39 $this->layoutTempstoreRepository = $layout_tempstore_repository;
40 $this->classResolver = $class_resolver;
46 public static function create(ContainerInterface $container) {
48 $container->get('layout_builder.tempstore_repository'),
49 $container->get('class_resolver')
54 * Adds the new section.
56 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
57 * The section storage.
59 * The delta of the section to splice.
60 * @param string $plugin_id
61 * The plugin ID of the layout to add.
63 * @return \Symfony\Component\HttpFoundation\Response
64 * The controller response.
66 public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
67 $section_storage->insertSection($delta, new Section($plugin_id));
69 $this->layoutTempstoreRepository->set($section_storage);
71 if ($this->isAjax()) {
72 return $this->rebuildAndClose($section_storage);
75 $url = $section_storage->getLayoutBuilderUrl();
76 return new RedirectResponse($url->setAbsolute()->toString());