diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 83b626b0f6..103431ba2f 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -28,14 +28,6 @@ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements La use SectionStorageTrait; - /** - * {@inheritdoc} - */ - public function __construct(array $values, $entity_type) { - parent::__construct($values, $entity_type); - $this->setSections($this->getThirdPartySetting('layout_builder', 'sections', [])); - } - /** * {@inheritdoc} */ @@ -51,14 +43,27 @@ public function setOverridable($overridable = TRUE) { return $this; } + /** + * {@inheritdoc} + */ + public function getSections() { + return $this->getThirdPartySetting('layout_builder', 'sections', []); + } + + /** + * {@inheritdoc} + */ + protected function setSections(array $sections) { + $this->setThirdPartySetting('layout_builder', 'sections', array_values($sections)); + return $this; + } + /** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); - $this->setThirdPartySetting('layout_builder', 'sections', $this->getSections()); - $original_value = isset($this->original) ? $this->original->isOverridable() : FALSE; $new_value = $this->isOverridable(); if ($original_value !== $new_value) { diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php index 05ea43e67d..a2ceca1373 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php @@ -20,26 +20,28 @@ class LayoutSectionItemList extends FieldItemList implements SectionListInterfac /** * {@inheritdoc} */ - public function setValue($values, $notify = TRUE) { - parent::setValue($values, $notify); - - $this->sections = []; + public function getSections() { + $sections = []; /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */ foreach ($this->list as $delta => $item) { - $this->sections[$delta] = $item->section; + $sections[$delta] = $item->section; } + return $sections; } /** * {@inheritdoc} */ - public function getValue() { + protected function setSections(array $sections) { $this->list = []; - foreach ($this->getSections() as $section) { - $this->appendItem()->section = $section; + $sections = array_values($sections); + /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */ + foreach ($sections as $section) { + $item = $this->appendItem(); + $item->section = $section; } - return parent::getValue(); + return $this; } /** diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php index d6072ce73d..c53211a29c 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php @@ -199,7 +199,7 @@ protected function getEntityTypes() { /** * {@inheritdoc} */ - public function convert($value, array $defaults) { + public function convert($value, $definition, $name, array $defaults) { if (!$value) { // If a bundle is not provided but a value corresponding to the bundle key // is, use that for the bundle value. diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index b2af10af66..82d7aa2a6e 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -108,7 +108,7 @@ public function getStorageId() { /** * {@inheritdoc} */ - public function convert($value, array $defaults) { + public function convert($value, $definition, $name, array $defaults) { if (strpos($value, ':') !== FALSE) { list($entity_type_id, $entity_id) = explode(':', $value); } diff --git a/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php b/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php index a727528076..263b767f72 100644 --- a/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php +++ b/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php @@ -46,7 +46,7 @@ public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore */ public function convert($value, $definition, $name, array $defaults) { if (isset($defaults['section_storage_type']) && $this->sectionStorageManager->hasDefinition($defaults['section_storage_type'])) { - if ($section_storage = $this->sectionStorageManager->loadFromRoute($defaults['section_storage_type'], $value, $defaults)) { + if ($section_storage = $this->sectionStorageManager->loadFromRoute($defaults['section_storage_type'], $value, $definition, $name, $defaults)) { // Pass the plugin through the tempstore repository. return $this->layoutTempstoreRepository->get($section_storage); } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php index 0439b8dd2e..03c7401ca1 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php @@ -54,9 +54,9 @@ public function loadFromObject($id, SectionListInterface $section_list) { /** * {@inheritdoc} */ - public function loadFromRoute($id, $value, array $defaults) { + public function loadFromRoute($id, $value, $definition, $name, array $defaults) { $plugin = $this->createInstance($id); - if ($section_list = $plugin->convert($value, $defaults)) { + if ($section_list = $plugin->convert($value, $definition, $name, $defaults)) { return $plugin->setSectionList($section_list); } } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php index 370545ee33..732de503ff 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -46,6 +46,10 @@ public function loadFromObject($id, SectionListInterface $section_list); * The ID of the section storage being instantiated. * @param string $value * The raw value. + * @param mixed $definition + * The parameter definition provided in the route options. + * @param string $name + * The name of the parameter. * @param array $defaults * The route defaults array. * @@ -54,6 +58,6 @@ public function loadFromObject($id, SectionListInterface $section_list); * * @see \Drupal\Core\ParamConverter\ParamConverterInterface::convert() */ - public function loadFromRoute($id, $value, array $defaults); + public function loadFromRoute($id, $value, $definition, $name, array $defaults); } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php index 5858c5021b..9d942c7ad8 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php @@ -14,38 +14,24 @@ */ trait SectionStorageTrait { - /** - * An array of section objects. - * - * @var \Drupal\layout_builder\Section[] - */ - protected $sections = []; - /** * Stores the information for all sections. * + * Implementations of this method are expected to call array_values() to rekey + * the list of sections. + * * @param \Drupal\layout_builder\Section[] $sections * An array of section objects. * * @return $this */ - protected function setSections(array $sections) { - $this->sections = array_values($sections); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getSections() { - return $this->sections; - } + abstract protected function setSections(array $sections); /** * {@inheritdoc} */ public function count() { - return count($this->sections); + return count($this->getSections()); } /** @@ -56,7 +42,7 @@ public function getSection($delta) { throw new \OutOfBoundsException(sprintf('Invalid delta "%s"', $delta)); } - return $this->sections[$delta]; + return $this->getSections()[$delta]; } /** @@ -70,8 +56,9 @@ public function getSection($delta) { * @return $this */ protected function setSection($delta, Section $section) { - $this->sections[$delta] = $section; - $this->setSections($this->sections); + $sections = $this->getSections(); + $sections[$delta] = $section; + $this->setSections($sections); return $this; } @@ -91,8 +78,8 @@ public function appendSection(Section $section) { public function insertSection($delta, Section $section) { if ($this->hasSection($delta)) { // @todo Use https://www.drupal.org/node/66183 once resolved. - $start = array_slice($this->sections, 0, $delta); - $end = array_slice($this->sections, $delta); + $start = array_slice($this->getSections(), 0, $delta); + $end = array_slice($this->getSections(), $delta); $this->setSections(array_merge($start, [$section], $end)); } else { @@ -121,7 +108,7 @@ public function removeSection($delta) { * TRUE if there is a section for this delta, FALSE otherwise. */ protected function hasSection($delta) { - return isset($this->sections[$delta]); + return isset($this->getSections()[$delta]); } } diff --git a/core/modules/layout_builder/src/SectionStorageInterface.php b/core/modules/layout_builder/src/SectionStorageInterface.php index 3f5bd95e9f..e3036f20c5 100644 --- a/core/modules/layout_builder/src/SectionStorageInterface.php +++ b/core/modules/layout_builder/src/SectionStorageInterface.php @@ -75,13 +75,19 @@ public function getLayoutBuilderUrl(); * * @param mixed $value * The raw value. + * @param mixed $definition + * The parameter definition provided in the route options. + * @param string $name + * The name of the parameter. * @param array $defaults * The route defaults array. * * @return \Drupal\layout_builder\SectionStorageInterface * The section storage if it could be loaded, or NULL otherwise. + * + * @see \Drupal\Core\ParamConverter\ParamConverterInterface::convert() */ - public function convert($value, array $defaults); + public function convert($value, $definition, $name, array $defaults); /** * Provides any available contexts for the object using the sections. diff --git a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php index 3d9dc15fd6..5922479ada 100644 --- a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php @@ -59,7 +59,7 @@ public function testConvert($success, $expected_entity_id, $value, array $defaul $this->entityTypeManager->getStorage('entity_view_display')->shouldNotBeCalled(); } - $result = $this->plugin->convert($value, $defaults); + $result = $this->plugin->convert($value, [], 'the_parameter_name', $defaults); if ($success) { $this->assertEquals('the_return_value', $result); } @@ -128,7 +128,7 @@ public function testConvertCreate() { $this->entityTypeManager->getDefinition('entity_view_display')->willReturn(new EntityType(['id' => 'entity_view_display'])); $this->entityTypeManager->getStorage('entity_view_display')->willReturn($entity_storage->reveal()); - $result = $this->plugin->convert($value, [], 'the_parameter_name', []); + $result = $this->plugin->convert($value, [], 'the_parameter_name', [], 'the_parameter_name', []); $this->assertSame($expected, $result); } diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreParamConverterTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreParamConverterTest.php index 70e0e19226..5f8dc051e3 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreParamConverterTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutTempstoreParamConverterTest.php @@ -32,7 +32,7 @@ public function testConvert() { $expected = 'the_return_value'; $section_storage_manager->hasDefinition('my_type')->willReturn(TRUE); - $section_storage_manager->loadFromRoute('my_type', $value, $defaults)->willReturn($section_storage); + $section_storage_manager->loadFromRoute('my_type', $value, $definition, $name, $defaults)->willReturn($section_storage); $layout_tempstore_repository->get($section_storage->reveal())->willReturn($expected); diff --git a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php index 72f9c29d42..0ffad1874d 100644 --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -72,7 +72,7 @@ public function testConvert($success, $expected_entity_type_id, $value, array $d $this->entityTypeManager->getStorage(Argument::any())->shouldNotBeCalled(); } - $result = $this->plugin->convert($value, $defaults); + $result = $this->plugin->convert($value, [], 'the_parameter_name', $defaults); if ($success) { $this->assertEquals('the_return_value', $result); }