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