diff --git a/core/modules/layout_builder/src/Controller/MoveBlockController.php b/core/modules/layout_builder/src/Controller/MoveBlockController.php index 096b314c4a..9a2401e30c 100644 --- a/core/modules/layout_builder/src/Controller/MoveBlockController.php +++ b/core/modules/layout_builder/src/Controller/MoveBlockController.php @@ -88,10 +88,10 @@ public function build(EntityInterface $entity, $delta_from, $delta_to, $region_f // block to the front. $block['region'] = $region_to; if (isset($preceding_block_uuid)) { - $section->insertBlock($block_uuid, $block, $preceding_block_uuid); + $section->insertBlockAfter($block_uuid, $block, $preceding_block_uuid); } else { - $section->addBlock($block_uuid, $block); + $section->appendBlock($block_uuid, $block); } $field->updateFromSection($section); diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php index 7dc6a2a87d..095580b666 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php @@ -27,14 +27,14 @@ public function appendSection(Section $section) { /** * {@inheritdoc} */ - public function addSection($index, Section $section) { - if ($this->get($index)) { + public function addSection($delta, Section $section) { + if ($this->get($delta)) { /** @var \Drupal\layout_builder\Field\LayoutSectionItemInterface $item */ - $item = $this->createItem($index); + $item = $this->createItem($delta); $item->updateFromSection($section); - $start = array_slice($this->list, 0, $index); - $end = array_slice($this->list, $index); + $start = array_slice($this->list, 0, $delta); + $end = array_slice($this->list, $delta); $this->list = array_merge($start, [$item], $end); } else { diff --git a/core/modules/layout_builder/src/Form/AddBlockForm.php b/core/modules/layout_builder/src/Form/AddBlockForm.php index 0e60ecc72a..466c8d7011 100644 --- a/core/modules/layout_builder/src/Form/AddBlockForm.php +++ b/core/modules/layout_builder/src/Form/AddBlockForm.php @@ -29,7 +29,7 @@ protected function submitLabel() { * {@inheritdoc} */ protected function submitBlock(Section $section, $uuid, array $block_info) { - $section->addBlock($uuid, $block_info); + $section->appendBlock($uuid, $block_info); } } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 606d04b460..f2835e3dda 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -60,7 +60,7 @@ public function __construct($layout_id, array $layout_settings = [], array $bloc $this->setBlockInfo($uuid, $info); } else { - $this->addBlock($uuid, $info); + $this->appendBlock($uuid, $info); } } } @@ -249,7 +249,7 @@ public function removeBlock($uuid) { } /** - * Adds a block to the front of a region. + * Appends a block to the end of a region. * * @param string $uuid * The UUID of the block to add. @@ -258,7 +258,7 @@ public function removeBlock($uuid) { * * @return $this */ - public function addBlock($uuid, array $block_info) { + public function appendBlock($uuid, array $block_info) { $block_info['weight'] = $this->getNextHighestWeight($block_info['region']); $this->setBlockInfo($uuid, $block_info); return $this; @@ -309,13 +309,58 @@ protected function getBlocksInfoByRegion($region) { * @throws \InvalidArgumentException * Thrown when the expected region does not exist. */ - public function insertBlock($uuid, array $block_info, $preceding_uuid) { - $blocks = $this->getBlocksInfoByRegion($block_info['region']); - $slice_id = array_search($preceding_uuid, array_keys($blocks), TRUE); + public function insertBlockAfter($uuid, array $block_info, $preceding_uuid) { + $uuids = array_keys($this->getBlocksInfoByRegion($block_info['region'])); + $slice_id = array_search($preceding_uuid, $uuids, TRUE); if ($slice_id === FALSE) { throw new \InvalidArgumentException('Invalid preceding UUID'); } + return $this->insertBlock($uuid, $block_info, $preceding_uuid, $slice_id); + } + /** + * Inserts a block at a specified delta. + * + * @param string $uuid + * The UUID of the block to insert. + * @param array $block_info + * The block info. + * @param int $delta + * The zero-based delta in which to insert the block. + * + * @return $this + * + * @throws \OutOfBoundsException + * Thrown when the specified delta is invalid. + */ + public function insertBlockAtDelta($uuid, array $block_info, $delta) { + $uuids = array_keys($this->getBlocksInfoByRegion($block_info['region'])); + if (!isset($uuids[$delta])) { + if ($delta === $this->getNextHighestWeight($block_info['region'])) { + return $this->appendBlock($uuid, $block_info); + } + + throw new \OutOfBoundsException('Invalid delta'); + } + return $this->insertBlock($uuid, $block_info, $uuids[$delta], $delta); + } + + /** + * Helper method for inserting a new block at a specified delta. + * + * @param string $uuid + * The UUID of the block to insert. + * @param array $block_info + * The block info. + * @param string $preceding_uuid + * The UUID of the existing block to insert after. + * @param int $slice_id + * The delta in which to insert the block. + * + * @return $this + */ + protected function insertBlock($uuid, array $block_info, $preceding_uuid, $slice_id) { + $blocks = $this->getBlocksInfoByRegion($block_info['region']); // Only process the blocks that need new weights. $blocks_to_set = [$uuid => $block_info] + array_slice($blocks, $slice_id + 1); diff --git a/core/modules/layout_builder/tests/src/Unit/SectionTest.php b/core/modules/layout_builder/tests/src/Unit/SectionTest.php index e9ccbad1e6..49398e774d 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionTest.php @@ -219,12 +219,12 @@ public function testRemoveBlock() { } /** - * @covers ::addBlock + * @covers ::appendBlock * @covers ::getNextHighestWeight * @covers ::getBlocksInfoByRegion */ - public function testAddBlock() { - $this->section->addBlock('new-uuid', [ + public function testAppendBlock() { + $this->section->appendBlock('new-uuid', [ 'configuration' => [], 'region' => 'some-region', ]); @@ -262,11 +262,11 @@ public function testAddBlock() { } /** + * @covers ::insertBlockAfter * @covers ::insertBlock - * @covers ::getBlocksInfoByRegion */ - public function testInsertBlock() { - $this->section->insertBlock('new-uuid', [ + public function testInsertBlockAfter() { + $this->section->insertBlockAfter('new-uuid', [ 'configuration' => [], 'region' => 'ordered-region', ], 'first-uuid'); @@ -304,27 +304,122 @@ public function testInsertBlock() { } /** - * @covers ::insertBlock + * @covers ::insertBlockAfter */ - public function testInsertBlockValidUuidRegionMismatch() { + public function testInsertBlockAfterValidUuidRegionMismatch() { $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); - $this->section->insertBlock('new-uuid', [ + $this->section->insertBlockAfter('new-uuid', [ 'configuration' => [], 'region' => 'ordered-region', ], 'existing-uuid'); } /** - * @covers ::insertBlock + * @covers ::insertBlockAfter */ - public function testInsertBlockInvalidUuid() { + public function testInsertBlockAfterInvalidUuid() { $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); - $this->section->insertBlock('new-uuid', [ + $this->section->insertBlockAfter('new-uuid', [ 'configuration' => [], 'region' => 'ordered-region', ], 'invalid-uuid'); } + /** + * @covers ::insertBlockAtDelta + * @covers ::insertBlock + */ + public function testInsertBlockAtDelta() { + $this->section->insertBlockAtDelta('new-uuid', [ + 'configuration' => [], + 'region' => 'ordered-region', + ], 0); + $expected = [ + 'existing-uuid' => [ + 'configuration' => [ + 'id' => 'existing-block-id', + ], + 'region' => 'some-region', + 'weight' => 0, + ], + 'first-uuid' => [ + 'configuration' => [ + 'id' => 'first-block-id', + ], + 'region' => 'ordered-region', + 'weight' => 0, + ], + 'new-uuid' => [ + 'configuration' => [], + 'region' => 'ordered-region', + 'weight' => 1, + ], + 'second-uuid' => [ + 'configuration' => [ + 'id' => 'second-block-id', + ], + 'region' => 'ordered-region', + 'weight' => 2, + ], + ]; + $result = $this->section->getBlocksInfo(); + $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); + } + + /** + * @covers ::insertBlockAtDelta + * @covers ::insertBlock + */ + public function testInsertBlockAtDeltaAppend() { + $this->section->insertBlockAtDelta('new-uuid', [ + 'configuration' => [], + 'region' => 'ordered-region', + ], 2); + $expected = [ + 'existing-uuid' => [ + 'configuration' => [ + 'id' => 'existing-block-id', + ], + 'region' => 'some-region', + 'weight' => 0, + ], + 'first-uuid' => [ + 'configuration' => [ + 'id' => 'first-block-id', + ], + 'region' => 'ordered-region', + 'weight' => 0, + ], + 'second-uuid' => [ + 'configuration' => [ + 'id' => 'second-block-id', + ], + 'region' => 'ordered-region', + 'weight' => 1, + ], + 'new-uuid' => [ + 'configuration' => [], + 'region' => 'ordered-region', + 'weight' => 2, + ], + ]; + $result = $this->section->getBlocksInfo(); + $this->assertEquals($expected, $result); + $this->assertSame($expected, $result); + } + + /** + * @covers ::insertBlockAtDelta + */ + public function testInsertBlockAtDeltaInvalidDelta() { + $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta'); + $this->section->insertBlockAtDelta('new-uuid', [ + 'configuration' => [], + 'region' => 'ordered-region', + ], 7); + } + /** * @covers ::updateBlock */