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