Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / Controller / AddSectionController.php
1 <?php
2
3 namespace Drupal\layout_builder\Controller;
4
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;
13
14 /**
15  * Defines a controller to add a new section.
16  *
17  * @internal
18  */
19 class AddSectionController implements ContainerInjectionInterface {
20
21   use AjaxHelperTrait;
22   use LayoutRebuildTrait;
23
24   /**
25    * The layout tempstore repository.
26    *
27    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
28    */
29   protected $layoutTempstoreRepository;
30
31   /**
32    * AddSectionController constructor.
33    *
34    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
35    *   The layout tempstore repository.
36    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
37    *   The class resolver.
38    */
39   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
40     $this->layoutTempstoreRepository = $layout_tempstore_repository;
41     $this->classResolver = $class_resolver;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('layout_builder.tempstore_repository'),
50       $container->get('class_resolver')
51     );
52   }
53
54   /**
55    * Adds the new section.
56    *
57    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
58    *   The section storage.
59    * @param int $delta
60    *   The delta of the section to splice.
61    * @param string $plugin_id
62    *   The plugin ID of the layout to add.
63    *
64    * @return \Symfony\Component\HttpFoundation\Response
65    *   The controller response.
66    */
67   public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
68     $section_storage->insertSection($delta, new Section($plugin_id));
69
70     $this->layoutTempstoreRepository->set($section_storage);
71
72     if ($this->isAjax()) {
73       return $this->rebuildAndClose($section_storage);
74     }
75     else {
76       $url = $section_storage->getLayoutBuilderUrl();
77       return new RedirectResponse($url->setAbsolute()->toString());
78     }
79   }
80
81 }