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