Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / Controller / MoveBlockController.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\SectionStorageInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines a controller to move a block.
13  *
14  * @internal
15  */
16 class MoveBlockController implements ContainerInjectionInterface {
17
18   use LayoutRebuildTrait;
19
20   /**
21    * The layout tempstore repository.
22    *
23    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
24    */
25   protected $layoutTempstoreRepository;
26
27   /**
28    * LayoutController constructor.
29    *
30    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
31    *   The layout tempstore repository.
32    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
33    *   The class resolver.
34    */
35   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
36     $this->layoutTempstoreRepository = $layout_tempstore_repository;
37     $this->classResolver = $class_resolver;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function create(ContainerInterface $container) {
44     return new static(
45       $container->get('layout_builder.tempstore_repository'),
46       $container->get('class_resolver')
47     );
48   }
49
50   /**
51    * Moves a block to another region.
52    *
53    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
54    *   The section storage.
55    * @param int $delta_from
56    *   The delta of the original section.
57    * @param int $delta_to
58    *   The delta of the destination section.
59    * @param string $region_to
60    *   The new region for this block.
61    * @param string $block_uuid
62    *   The UUID for this block.
63    * @param string|null $preceding_block_uuid
64    *   (optional) If provided, the UUID of the block to insert this block after.
65    *
66    * @return \Drupal\Core\Ajax\AjaxResponse
67    *   An AJAX response.
68    */
69   public function build(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
70     $section = $section_storage->getSection($delta_from);
71
72     $component = $section->getComponent($block_uuid);
73     $section->removeComponent($block_uuid);
74
75     // If the block is moving from one section to another, update the original
76     // section and load the new one.
77     if ($delta_from !== $delta_to) {
78       $section = $section_storage->getSection($delta_to);
79     }
80
81     // If a preceding block was specified, insert after that. Otherwise add the
82     // block to the front.
83     $component->setRegion($region_to);
84     if (isset($preceding_block_uuid)) {
85       $section->insertAfterComponent($preceding_block_uuid, $component);
86     }
87     else {
88       $section->insertComponent(0, $component);
89     }
90
91     $this->layoutTempstoreRepository->set($section_storage);
92     return $this->rebuildLayout($section_storage);
93   }
94
95 }