3 namespace Drupal\layout_builder\Controller;
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;
12 * Defines a controller to move a block.
16 class MoveBlockController implements ContainerInjectionInterface {
18 use LayoutRebuildTrait;
21 * The layout tempstore repository.
23 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
25 protected $layoutTempstoreRepository;
28 * LayoutController constructor.
30 * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
31 * The layout tempstore repository.
32 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
35 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
36 $this->layoutTempstoreRepository = $layout_tempstore_repository;
37 $this->classResolver = $class_resolver;
43 public static function create(ContainerInterface $container) {
45 $container->get('layout_builder.tempstore_repository'),
46 $container->get('class_resolver')
51 * Moves a block to another region.
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.
66 * @return \Drupal\Core\Ajax\AjaxResponse
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);
72 $component = $section->getComponent($block_uuid);
73 $section->removeComponent($block_uuid);
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);
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);
88 $section->insertComponent(0, $component);
91 $this->layoutTempstoreRepository->set($section_storage);
92 return $this->rebuildLayout($section_storage);