diff --git a/core/modules/layout_builder/src/Controller/MoveBlockController.php b/core/modules/layout_builder/src/Controller/MoveBlockController.php index d1b26a2534..e014fc5ab9 100644 --- a/core/modules/layout_builder/src/Controller/MoveBlockController.php +++ b/core/modules/layout_builder/src/Controller/MoveBlockController.php @@ -93,10 +93,10 @@ public function build(EntityInterface $entity, $delta_from, $delta_to, $region_f // If a preceding block was specified, insert after that. Otherwise add the // block to the front. if (isset($preceding_block_uuid)) { - $section->insertBlock($region_to, [$block_uuid => $block], $preceding_block_uuid); + $section->insertBlock($region_to, $block_uuid, $block, $preceding_block_uuid); } else { - $section->addBlock($region_to, [$block_uuid => $block]); + $section->addBlock($region_to, $block_uuid, $block); } $field->updateFromSection($section); diff --git a/core/modules/layout_builder/src/Form/AddBlockForm.php b/core/modules/layout_builder/src/Form/AddBlockForm.php index e55106cd57..1757606002 100644 --- a/core/modules/layout_builder/src/Form/AddBlockForm.php +++ b/core/modules/layout_builder/src/Form/AddBlockForm.php @@ -2,6 +2,8 @@ namespace Drupal\layout_builder\Form; +use Drupal\layout_builder\Section; + /** * Provides a form to add a block. * @@ -23,4 +25,11 @@ protected function submitLabel() { return $this->t('Add Block'); } + /** + * {@inheritdoc} + */ + protected function submitBlock(Section $section, $region, $uuid, array $configuration) { + $section->addBlock($region, $uuid, $configuration); + } + } diff --git a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php index a7539611cc..de6b878328 100644 --- a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php +++ b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php @@ -17,6 +17,7 @@ use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\layout_builder\Controller\LayoutRebuildFormTrait; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; +use Drupal\layout_builder\Section; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -212,6 +213,20 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt */ abstract protected function submitLabel(); + /** + * Handles the submission of a block. + * + * @param \Drupal\layout_builder\Section $section + * The layout section. + * @param string $region + * The region name. + * @param string $uuid + * The UUID of the block. + * @param array $configuration + * The block configuration. + */ + abstract protected function submitBlock(Section $section, $region, $uuid, array $configuration); + /** * {@inheritdoc} */ @@ -238,8 +253,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\layout_builder\Field\LayoutSectionItemInterface $field */ $field = $this->entity->layout_builder__layout->get($this->delta); $section = $field->getSection(); - $block = &$section->getBlock($this->region, $configuration['uuid']); - $block['block'] = $configuration; + $this->submitBlock($section, $this->region, $configuration['uuid'], ['block' => $configuration]); $field->updateFromSection($section); $this->layoutTempstoreRepository->set($this->entity); diff --git a/core/modules/layout_builder/src/Form/UpdateBlockForm.php b/core/modules/layout_builder/src/Form/UpdateBlockForm.php index bfef3dc020..2f2aa600e4 100644 --- a/core/modules/layout_builder/src/Form/UpdateBlockForm.php +++ b/core/modules/layout_builder/src/Form/UpdateBlockForm.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\layout_builder\Section; /** * Provides a form to update a block. @@ -56,4 +57,11 @@ protected function submitLabel() { return $this->t('Update'); } + /** + * {@inheritdoc} + */ + protected function submitBlock(Section $section, $region, $uuid, array $configuration) { + $section->updateBlock($region, $uuid, $configuration); + } + } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index ba4307e1f9..f5e19003b5 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -51,7 +51,7 @@ public function getValue() { * @throws \InvalidArgumentException * Thrown when the expected region or UUID do not exist. */ - public function &getBlock($region, $uuid) { + public function getBlock($region, $uuid) { if (!isset($this->section[$region])) { throw new \InvalidArgumentException('Invalid region'); } @@ -70,7 +70,7 @@ public function &getBlock($region, $uuid) { * The region name. * @param string $uuid * The UUID of the block to retrieve. - * @param array $block + * @param array $configuration * The block configuration. * * @return $this @@ -78,7 +78,7 @@ public function &getBlock($region, $uuid) { * @throws \InvalidArgumentException * Thrown when the expected region or UUID do not exist. */ - public function updateBlock($region, $uuid, array $block) { + public function updateBlock($region, $uuid, array $configuration) { if (!isset($this->section[$region])) { throw new \InvalidArgumentException('Invalid region'); } @@ -87,7 +87,7 @@ public function updateBlock($region, $uuid, array $block) { throw new \InvalidArgumentException('Invalid UUID'); } - $this->section[$region][$uuid] = $block; + $this->section[$region][$uuid] = $configuration; return $this; } @@ -113,14 +113,16 @@ public function removeBlock($region, $uuid) { * * @param string $region * The region name. - * @param array $block - * The block. + * @param string $uuid + * The UUID of the block to add. + * @param array $configuration + * The block configuration. * * @return $this */ - public function addBlock($region, array $block) { + public function addBlock($region, $uuid, array $configuration) { $this->section += [$region => []]; - $this->section[$region] = array_merge($block, $this->section[$region]); + $this->section[$region] = array_merge([$uuid => $configuration], $this->section[$region]); return $this; } @@ -129,8 +131,10 @@ public function addBlock($region, array $block) { * * @param string $region * The region name. - * @param array $block - * The block to insert. + * @param string $uuid + * The UUID of the block to insert. + * @param array $configuration + * The block configuration. * @param string $preceding_uuid * The UUID of the existing block to insert after. * @@ -139,7 +143,7 @@ public function addBlock($region, array $block) { * @throws \InvalidArgumentException * Thrown when the expected region does not exist. */ - public function insertBlock($region, array $block, $preceding_uuid) { + public function insertBlock($region, $uuid, array $configuration, $preceding_uuid) { if (!isset($this->section[$region])) { throw new \InvalidArgumentException('Invalid region'); } @@ -151,7 +155,7 @@ public function insertBlock($region, array $block, $preceding_uuid) { $before = array_slice($this->section[$region], 0, $slice_id + 1); $after = array_slice($this->section[$region], $slice_id + 1); - $this->section[$region] = array_merge($before, $block, $after); + $this->section[$region] = array_merge($before, [$uuid => $configuration], $after); return $this; } diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutSectionTest.php deleted file mode 100644 index 711f38dfd5..0000000000 --- a/core/modules/layout_builder/tests/src/Unit/LayoutSectionTest.php +++ /dev/null @@ -1,130 +0,0 @@ - [], - 'some-region' => [ - 'some-uuid' => [ - 'block' => [ - 'id' => 'some-block-id', - ], - ], - ], - ]; - $section = new Section($expected); - $this->assertSame($expected, $section->getValue()); - } - - /** - * @covers ::getBlock - */ - public function testGetBlockInvalidRegion() { - $section = new Section([]); - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid region'); - $section->getBlock('invalid', 'valid-uuid'); - } - - /** - * @covers ::getBlock - */ - public function testGetBlockInvalidUuid() { - $section = new Section(['top' => []]); - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID'); - $section->getBlock('top', 'invalid-uuid'); - } - - /** - * @covers ::getBlock - */ - public function testGetBlock() { - $section = new Section(['top' => ['valid-uuid' => ['block' => ['id' => 'some_block_id']]]]); - $expected = ['block' => ['id' => 'some_block_id']]; - - $block = $section->getBlock('top', 'valid-uuid'); - $this->assertSame($expected, $block); - } - - /** - * @covers ::removeBlock - */ - public function testRemoveBlock() { - $section = new Section(['top' => ['existing-uuid' => []]]); - $section->removeBlock('top', 'existing-uuid'); - $expected = []; - $this->assertSame($expected, $section->getValue()); - } - - /** - * @covers ::addBlock - */ - public function testAddBlock() { - $section = new Section(['top' => ['existing-uuid' => []]]); - $section->addBlock('top', ['new-uuid' => []]); - $expected = [ - 'top' => [ - 'new-uuid' => [], - 'existing-uuid' => [], - ], - ]; - $this->assertSame($expected, $section->getValue()); - } - - /** - * @covers ::insertBlock - */ - public function testInsertBlock() { - $section = new Section([ - 'top' => [ - 'first-uuid' => [], - 'second-uuid' => [], - ], - ]); - $section->insertBlock('top', ['new-uuid' => []], 'first-uuid'); - $expected = [ - 'top' => [ - 'first-uuid' => [], - 'new-uuid' => [], - 'second-uuid' => [], - ], - ]; - $this->assertSame($expected, $section->getValue()); - } - - /** - * @covers ::insertBlock - */ - public function testInsertBlockInvalidRegion() { - $section = new Section([]); - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid region'); - $section->insertBlock('top', [], 'first-uuid'); - } - - /** - * @covers ::insertBlock - */ - public function testInsertBlockInvalidUuid() { - $section = new Section([ - 'top' => [ - 'second-uuid' => [], - ], - ]); - $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); - $section->insertBlock('top', [], 'first-uuid'); - } - -} diff --git a/core/modules/layout_builder/tests/src/Unit/SectionTest.php b/core/modules/layout_builder/tests/src/Unit/SectionTest.php new file mode 100644 index 0000000000..33a338706e --- /dev/null +++ b/core/modules/layout_builder/tests/src/Unit/SectionTest.php @@ -0,0 +1,265 @@ +section = new Section([ + 'empty-region' => [], + 'some-region' => [ + 'existing-uuid' => [ + 'block' => [ + 'id' => 'existing-block-id', + ], + ], + ], + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]); + } + + /** + * @covers ::__construct + * @covers ::getValue + */ + public function testGetValue() { + $expected = [ + 'empty-region' => [], + 'some-region' => [ + 'existing-uuid' => [ + 'block' => [ + 'id' => 'existing-block-id', + ], + ], + ], + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]; + $result = $this->section->getValue(); + $this->assertSame($expected, $result); + } + + /** + * @covers ::getBlock + */ + public function testGetBlockInvalidRegion() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid region'); + $this->section->getBlock('invalid-region', 'existing-uuid'); + } + + /** + * @covers ::getBlock + */ + public function testGetBlockInvalidUuid() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID'); + $this->section->getBlock('some-region', 'invalid-uuid'); + } + + /** + * @covers ::getBlock + */ + public function testGetBlock() { + $expected = ['block' => ['id' => 'existing-block-id']]; + + $block = $this->section->getBlock('some-region', 'existing-uuid'); + $this->assertSame($expected, $block); + } + + /** + * @covers ::removeBlock + */ + public function testRemoveBlock() { + $this->section->removeBlock('some-region', 'existing-uuid'); + $expected = [ + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]; + $this->assertSame($expected, $this->section->getValue()); + } + + /** + * @covers ::addBlock + */ + public function testAddBlock() { + $this->section->addBlock('some-region', 'new-uuid', []); + $expected = [ + 'empty-region' => [], + 'some-region' => [ + 'new-uuid' => [], + 'existing-uuid' => [ + 'block' => [ + 'id' => 'existing-block-id', + ], + ], + ], + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]; + $this->assertSame($expected, $this->section->getValue()); + } + + /** + * @covers ::insertBlock + */ + public function testInsertBlock() { + $this->section->insertBlock('ordered-region', 'new-uuid', [], 'first-uuid'); + $expected = [ + 'empty-region' => [], + 'some-region' => [ + 'existing-uuid' => [ + 'block' => [ + 'id' => 'existing-block-id', + ], + ], + ], + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'new-uuid' => [], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]; + $this->assertSame($expected, $this->section->getValue()); + } + + /** + * @covers ::insertBlock + */ + public function testInsertBlockInvalidRegion() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid region'); + $this->section->insertBlock('invalid-region', 'new-uuid', [], 'first-uuid'); + } + + /** + * @covers ::insertBlock + */ + public function testInsertBlockInvalidUuid() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid preceding UUID'); + $this->section->insertBlock('ordered-region', 'new-uuid', [], 'invalid-uuid'); + } + + /** + * @covers ::updateBlock + */ + public function testUpdateBlock() { + $this->section->updateBlock('some-region', 'existing-uuid', [ + 'block' => [ + 'id' => 'existing-block-id', + 'settings' => [ + 'foo' => 'bar', + ], + ], + ]); + + $expected = [ + 'empty-region' => [], + 'some-region' => [ + 'existing-uuid' => [ + 'block' => [ + 'id' => 'existing-block-id', + 'settings' => [ + 'foo' => 'bar', + ], + ], + ], + ], + 'ordered-region' => [ + 'first-uuid' => [ + 'block' => [ + 'id' => 'first-block-id', + ], + ], + 'second-uuid' => [ + 'block' => [ + 'id' => 'second-block-id', + ], + ], + ], + ]; + $this->assertSame($expected, $this->section->getValue()); + } + + /** + * @covers ::updateBlock + */ + public function testUpdateBlockInvalidRegion() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid region'); + $this->section->updateBlock('invalid-region', 'new-uuid', []); + } + + /** + * @covers ::updateBlock + */ + public function testUpdateBlockInvalidUuid() { + $this->setExpectedException(\InvalidArgumentException::class, 'Invalid UUID'); + $this->section->updateBlock('ordered-region', 'new-uuid', []); + } + +}