diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplayStorage.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplayStorage.php index e86df9daf840173346ee4fcbe86ea6e8182f19e0..e00a2b46306692c487d611cf297c971511f020a7 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplayStorage.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplayStorage.php @@ -5,7 +5,6 @@ use Drupal\Core\Config\Entity\ConfigEntityStorage; use Drupal\Core\Entity\EntityInterface; use Drupal\layout_builder\Section; -use Drupal\layout_builder\SectionComponent; /** * Provides storage for entity view display entities that have layouts. @@ -38,20 +37,7 @@ protected function mapFromStorageRecords(array $records) { foreach ($records as $id => &$record) { if (!empty($record['third_party_settings']['layout_builder']['sections'])) { $sections = &$record['third_party_settings']['layout_builder']['sections']; - foreach ($sections as $section_delta => $section) { - $sections[$section_delta] = new Section( - $section['layout_id'], - $section['layout_settings'], - array_map(function (array $component) { - return (new SectionComponent( - $component['uuid'], - $component['region'], - $component['configuration'], - $component['additional'] - ))->setWeight($component['weight']); - }, $section['components']) - ); - } + $sections = array_map([Section::class, 'fromArray'], $sections); } } return parent::mapFromStorageRecords($records); diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 04b1cbb2c60eccecec18650a60d4e0382ab2a25d..30d903a30ab8a8f15eb116389a41c385d833313c 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -322,8 +322,7 @@ protected function layoutPluginManager() { /** * Returns an array representation of the section. * - * @internal - * This is intended for use by a storage mechanism for sections. + * Only use this method if you are implementing custom storage for sections. * * @return array * An array representation of the section component. @@ -338,4 +337,23 @@ public function toArray() { ]; } + /** + * Creates an object from an array representation of the section. + * + * Only use this method if you are implementing custom storage for sections. + * + * @param array $section + * An array of section data in the format returned by ::toArray(). + * + * @return static + * The section object. + */ + public static function fromArray(array $section) { + return new static( + $section['layout_id'], + $section['layout_settings'], + array_map([SectionComponent::class, 'fromArray'], $section['components']) + ); + } + } diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 081d982f054b4dd50a12876a349acca5df6c0fa1..4937f7fbcde669fe61c644c043b2f787877bbdfd 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -292,8 +292,7 @@ protected function eventDispatcher() { /** * Returns an array representation of the section component. * - * @internal - * This is intended for use by a storage mechanism for section components. + * Only use this method if you are implementing custom storage for sections. * * @return array * An array representation of the section component. @@ -308,4 +307,24 @@ public function toArray() { ]; } + /** + * Creates an object from an array representation of the section component. + * + * Only use this method if you are implementing custom storage for sections. + * + * @param array $component + * An array of section component data in the format returned by ::toArray(). + * + * @return static + * The section component object. + */ + public static function fromArray(array $component) { + return (new static( + $component['uuid'], + $component['region'], + $component['configuration'], + $component['additional'] + ))->setWeight($component['weight']); + } + }