annotate core/modules/layout_builder/src/Controller/MoveBlockController.php @ 19:fa3358dc1485 tip

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