annotate core/modules/layout_builder/src/Controller/MoveBlockController.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
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\ClassResolverInterface;
Chris@14 6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Chris@14 7 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
Chris@14 8 use Drupal\layout_builder\SectionStorageInterface;
Chris@14 9 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@14 10
Chris@14 11 /**
Chris@14 12 * Defines a controller to move a block.
Chris@14 13 *
Chris@14 14 * @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 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
Chris@14 33 * The class resolver.
Chris@14 34 */
Chris@14 35 public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
Chris@14 36 $this->layoutTempstoreRepository = $layout_tempstore_repository;
Chris@14 37 $this->classResolver = $class_resolver;
Chris@14 38 }
Chris@14 39
Chris@14 40 /**
Chris@14 41 * {@inheritdoc}
Chris@14 42 */
Chris@14 43 public static function create(ContainerInterface $container) {
Chris@14 44 return new static(
Chris@14 45 $container->get('layout_builder.tempstore_repository'),
Chris@14 46 $container->get('class_resolver')
Chris@14 47 );
Chris@14 48 }
Chris@14 49
Chris@14 50 /**
Chris@14 51 * Moves a block to another region.
Chris@14 52 *
Chris@14 53 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
Chris@14 54 * The section storage.
Chris@14 55 * @param int $delta_from
Chris@14 56 * The delta of the original section.
Chris@14 57 * @param int $delta_to
Chris@14 58 * The delta of the destination section.
Chris@14 59 * @param string $region_to
Chris@14 60 * The new region for this block.
Chris@14 61 * @param string $block_uuid
Chris@14 62 * The UUID for this block.
Chris@14 63 * @param string|null $preceding_block_uuid
Chris@14 64 * (optional) If provided, the UUID of the block to insert this block after.
Chris@14 65 *
Chris@14 66 * @return \Drupal\Core\Ajax\AjaxResponse
Chris@14 67 * An AJAX response.
Chris@14 68 */
Chris@14 69 public function build(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
Chris@14 70 $section = $section_storage->getSection($delta_from);
Chris@14 71
Chris@14 72 $component = $section->getComponent($block_uuid);
Chris@14 73 $section->removeComponent($block_uuid);
Chris@14 74
Chris@14 75 // If the block is moving from one section to another, update the original
Chris@14 76 // section and load the new one.
Chris@14 77 if ($delta_from !== $delta_to) {
Chris@14 78 $section = $section_storage->getSection($delta_to);
Chris@14 79 }
Chris@14 80
Chris@14 81 // If a preceding block was specified, insert after that. Otherwise add the
Chris@14 82 // block to the front.
Chris@14 83 $component->setRegion($region_to);
Chris@14 84 if (isset($preceding_block_uuid)) {
Chris@14 85 $section->insertAfterComponent($preceding_block_uuid, $component);
Chris@14 86 }
Chris@14 87 else {
Chris@14 88 $section->insertComponent(0, $component);
Chris@14 89 }
Chris@14 90
Chris@14 91 $this->layoutTempstoreRepository->set($section_storage);
Chris@14 92 return $this->rebuildLayout($section_storage);
Chris@14 93 }
Chris@14 94
Chris@14 95 }