diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index f7841612d7..18ec1b457d 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -401,6 +401,8 @@ public function setComponent($name, array $options = []) { return $this; } + // Retrieve the updated options after the parent:: call. + $options = $this->content[$name]; // Provide backwards compatibility by converting to a section component. $field_definition = $this->getFieldDefinition($name); if ($field_definition && $field_definition->isDisplayConfigurable('view') && isset($options['type'])) { @@ -415,10 +417,7 @@ public function setComponent($name, array $options = []) { $configuration = []; $configuration['id'] = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $name; $configuration['label_display'] = FALSE; - $configuration['formatter']['type'] = $options['type']; - $configuration['formatter']['label'] = $options['label']; - $configuration['formatter']['settings'] = $options['settings']; - $configuration['formatter']['third_party_settings'] = $options['third_party_settings']; + $configuration['formatter'] = array_intersect_key($options, array_flip(['type', 'label', 'settings', 'third_party_settings'])); $configuration['context_mapping']['entity'] = 'layout_builder.entity'; $new_component = (new SectionComponent(\Drupal::service('uuid')->generate(), $options['region'], $configuration)); $section->appendComponent($new_component); diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php index a2312a4b58..a559368f3d 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\layout_builder\Unit; use Drupal\layout_builder\LayoutTempstoreRepository; +use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionStorageInterface; use Drupal\Tests\UnitTestCase; use Drupal\Core\TempStore\SharedTempStore; @@ -18,9 +19,7 @@ class LayoutTempstoreRepositoryTest extends UnitTestCase { * @covers ::get */ public function testGetEmptyTempstore() { - $section_storage = $this->prophesize(SectionStorageInterface::class); - $section_storage->getStorageType()->willReturn('my_storage_type'); - $section_storage->getStorageId()->willReturn('my_storage_id'); + $section_storage = new TestSectionStorage(); $tempstore = $this->prophesize(SharedTempStore::class); $tempstore->get('my_storage_id')->shouldBeCalled(); @@ -30,38 +29,34 @@ public function testGetEmptyTempstore() { $repository = new LayoutTempstoreRepository($tempstore_factory->reveal()); - $result = $repository->get($section_storage->reveal()); - $this->assertSame($section_storage->reveal(), $result); + $result = $repository->get($section_storage); + $this->assertSame($section_storage, $result); } /** * @covers ::get */ public function testGetLoadedTempstore() { - $section_storage = $this->prophesize(SectionStorageInterface::class); - $section_storage->getStorageType()->willReturn('my_storage_type'); - $section_storage->getStorageId()->willReturn('my_storage_id'); + $section_storage = new TestSectionStorage(); - $tempstore_section_storage = $this->prophesize(SectionStorageInterface::class); + $tempstore_section_storage = new TestSectionStorage(); $tempstore = $this->prophesize(SharedTempStore::class); - $tempstore->get('my_storage_id')->willReturn(['section_storage' => $tempstore_section_storage->reveal()]); + $tempstore->get('my_storage_id')->willReturn(['section_storage' => $tempstore_section_storage]); $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class); $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal()); $repository = new LayoutTempstoreRepository($tempstore_factory->reveal()); - $result = $repository->get($section_storage->reveal()); - $this->assertSame($tempstore_section_storage->reveal(), $result); - $this->assertNotSame($section_storage->reveal(), $result); + $result = $repository->get($section_storage); + $this->assertSame($tempstore_section_storage, $result); + $this->assertNotSame($section_storage, $result); } /** * @covers ::get */ public function testGetInvalidEntry() { - $section_storage = $this->prophesize(SectionStorageInterface::class); - $section_storage->getStorageType()->willReturn('my_storage_type'); - $section_storage->getStorageId()->willReturn('my_storage_id'); + $section_storage = new TestSectionStorage(); $tempstore = $this->prophesize(SharedTempStore::class); $tempstore->get('my_storage_id')->willReturn(['section_storage' => 'this_is_not_an_entity']); @@ -72,7 +67,36 @@ public function testGetInvalidEntry() { $repository = new LayoutTempstoreRepository($tempstore_factory->reveal()); $this->setExpectedException(\UnexpectedValueException::class, 'The entry with storage type "my_storage_type" and ID "my_storage_id" is invalid'); - $repository->get($section_storage->reveal()); + $repository->get($section_storage); } } + +/** + * Provides a test implementation of section storage. + * + * @todo This works around https://github.com/phpspec/prophecy/issues/119. + */ +class TestSectionStorage implements SectionStorageInterface { + + public static function getStorageType() { + return 'my_storage_type'; + } + + public function getStorageId() { + return 'my_storage_id'; + } + + public function count() {} + public function getSections() {} + public function getSection($delta) {} + public function appendSection(Section $section) {} + public function insertSection($delta, Section $section) {} + public function removeSection($delta) {} + public function getContexts() {} + public function label() {} + public function save() {} + public function getCanonicalUrl() {} + public function getLayoutBuilderUrl() {} + +}