diff --git a/core/modules/layout_builder/src/Plugin/DataType/SectionData.php b/core/modules/layout_builder/src/Plugin/DataType/SectionData.php index f5858cedac..353c53fe7c 100644 --- a/core/modules/layout_builder/src/Plugin/DataType/SectionData.php +++ b/core/modules/layout_builder/src/Plugin/DataType/SectionData.php @@ -28,7 +28,7 @@ class SectionData extends TypedData { */ public function setValue($value, $notify = TRUE) { if ($value && !$value instanceof Section) { - throw new \InvalidArgumentException('Value is not a valid section'); + throw new \InvalidArgumentException(sprintf('Value assigned to "%s" is not a valid section', $this->getName())); } parent::setValue($value, $notify); } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 677f4e9c27..f70679a4b4 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -258,7 +258,7 @@ public function insertComponent($delta, SectionComponent $new_component) { $components = $this->getComponentsByRegion($new_component->getRegion()); $count = count($components); if ($delta > $count) { - throw new \OutOfBoundsException('Invalid delta'); + throw new \OutOfBoundsException(sprintf('Invalid delta "%s" for the "%s" component', $delta, $new_component->getUuid())); } // If the delta is the end of the list, append the component instead. elseif ($delta === $count) { diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 4c8d924697..b681ba6b52 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -55,9 +55,9 @@ class SectionComponent { /** * The weight of the component. * - * @var int|null + * @var int */ - protected $weight; + protected $weight = 0; /** * Any additional properties and values. @@ -75,16 +75,13 @@ class SectionComponent { * The region. * @param mixed[] $configuration * The plugin configuration. - * @param int $weight - * The weight. If NULL, it must be set externally via ::setWeight(). * @param mixed[] $additional * An additional values. */ - public function __construct($uuid, $region, array $configuration = [], $weight = NULL, array $additional = []) { + public function __construct($uuid, $region, array $configuration = [], array $additional = []) { $this->uuid = $uuid; $this->region = $region; $this->configuration = $configuration; - $this->weight = $weight; $this->additional = $additional; } @@ -193,9 +190,6 @@ public function setRegion($region) { * Thrown if the weight was never set. */ public function getWeight() { - if (!is_int($this->weight)) { - throw new \UnexpectedValueException(sprintf('The component with UUID of "%s" is missing a weight', $this->getUuid())); - } return $this->weight; } diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php index fcf0326add..90ad29886a 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php @@ -64,7 +64,7 @@ public function providerTestLayoutSectionFormatter() { 'context_mapping' => [ 'user' => '@user.current_user_context:current_user', ], - ], 0), + ]), ]), ], ], @@ -86,7 +86,7 @@ public function providerTestLayoutSectionFormatter() { 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', - ], 0), + ]), ]), ], ], @@ -102,7 +102,7 @@ public function providerTestLayoutSectionFormatter() { 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', - ], 0), + ]), ]), ], [ @@ -110,11 +110,11 @@ public function providerTestLayoutSectionFormatter() { 'foo' => new SectionComponent('foo', 'first', [ 'id' => 'test_block_instantiation', 'display_message' => 'foo text', - ], 0), + ]), 'bar' => new SectionComponent('bar', 'second', [ 'id' => 'test_block_instantiation', 'display_message' => 'bar text', - ], 0), + ]), ]), ], ], @@ -158,7 +158,7 @@ public function testLayoutSectionFormatterAccess() { 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'test_access', - ], 0), + ]), ]), ], ]); @@ -192,7 +192,7 @@ public function testMultilingualLayoutSectionFormatter() { 'section' => new Section('layout_onecol', [], [ 'baz' => new SectionComponent('baz', 'content', [ 'id' => 'system_powered_by_block', - ], 0), + ]), ]), ], ]); @@ -204,11 +204,11 @@ public function testMultilingualLayoutSectionFormatter() { 'foo' => new SectionComponent('foo', 'first', [ 'id' => 'test_block_instantiation', 'display_message' => 'foo text', - ], 0), + ]), 'bar' => new SectionComponent('bar', 'second', [ 'id' => 'test_block_instantiation', 'display_message' => 'bar text', - ], 0), + ]), ]), ], ], diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php index 8163cb7e6d..28f22557e7 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutSectionBuilderTest.php @@ -136,7 +136,7 @@ public function testBuildSection() { $block->getConfiguration()->willReturn([]); $section = [ - new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id'], 0), + new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), ]; $expected = [ 'content' => [ @@ -160,7 +160,7 @@ public function testBuildSectionAccessDenied() { $block->build()->shouldNotBeCalled(); $section = [ - new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id'], 0), + new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), ]; $expected = [ 'content' => [ @@ -225,7 +225,7 @@ public function testContextAwareBlock() { $this->contextHandler->applyContextMapping($block->reveal(), [])->shouldBeCalled(); $section = [ - new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id'], 0), + new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), ]; $expected = [ 'content' => [ diff --git a/core/modules/layout_builder/tests/src/Unit/SectionTest.php b/core/modules/layout_builder/tests/src/Unit/SectionTest.php index 1803ee0f7f..eff239507d 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionTest.php @@ -26,9 +26,9 @@ protected function setUp() { parent::setUp(); $this->section = new Section('layout_onecol', [], [ - new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 3), - new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 2), + new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']), + (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3), + (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2), ]); } @@ -39,9 +39,9 @@ protected function setUp() { */ public function testGetComponents() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 3), - 'first-uuid' => new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 2), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3), + 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2), ]; $this->assertComponents($expected, $this->section); @@ -59,7 +59,7 @@ public function testGetComponentInvalidUuid() { * @covers ::getComponent */ public function testGetComponent() { - $expected = new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0); + $expected = new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']); $this->assertEquals($expected, $this->section->getComponent('existing-uuid')); } @@ -70,8 +70,8 @@ public function testGetComponent() { */ public function testRemoveComponent() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 3), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3), ]; $this->section->removeComponent('first-uuid'); @@ -85,10 +85,10 @@ public function testRemoveComponent() { */ public function testAppendComponent() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 3), - 'first-uuid' => new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 2), - 'new-uuid' => new SectionComponent('new-uuid', 'some-region', [], 1), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3), + 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2), + 'new-uuid' => (new SectionComponent('new-uuid', 'some-region', []))->setWeight(1), ]; $this->section->appendComponent(new SectionComponent('new-uuid', 'some-region')); @@ -100,10 +100,10 @@ public function testAppendComponent() { */ public function testInsertAfterComponent() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 4), - 'first-uuid' => new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 2), - 'new-uuid' => new SectionComponent('new-uuid', 'ordered-region', [], 3), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(4), + 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2), + 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(3), ]; $this->section->insertAfterComponent('first-uuid', new SectionComponent('new-uuid', 'ordered-region')); @@ -132,10 +132,10 @@ public function testInsertAfterComponentInvalidUuid() { */ public function testInsertComponent() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 4), - 'first-uuid' => new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 3), - 'new-uuid' => new SectionComponent('new-uuid', 'ordered-region', [], 2), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(4), + 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(3), + 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(2), ]; $this->section->insertComponent(0, new SectionComponent('new-uuid', 'ordered-region')); @@ -147,10 +147,10 @@ public function testInsertComponent() { */ public function testInsertComponentAppend() { $expected = [ - 'existing-uuid' => new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id'], 0), - 'second-uuid' => new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id'], 3), - 'first-uuid' => new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id'], 2), - 'new-uuid' => new SectionComponent('new-uuid', 'ordered-region', [], 4), + 'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']))->setWeight(0), + 'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3), + 'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2), + 'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(4), ]; $this->section->insertComponent(2, new SectionComponent('new-uuid', 'ordered-region')); @@ -161,7 +161,7 @@ public function testInsertComponentAppend() { * @covers ::insertComponent */ public function testInsertComponentInvalidDelta() { - $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta'); + $this->setExpectedException(\OutOfBoundsException::class, 'Invalid delta "7" for the "new-uuid" component'); $this->section->insertComponent(7, new SectionComponent('new-uuid', 'ordered-region')); }