annotate core/modules/layout_builder/src/Controller/MoveBlockController.php @ 5:12f9dff5fda9 tip

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