diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 30d903a30ab8..1ec0b8af10ae 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -356,4 +356,13 @@ class Section { ); } + /** + * Magic method: Implements a deep clone. + */ + public function __clone() { + foreach ($this->components as $uuid => $component) { + $this->components[$uuid] = clone $component; + } + } + } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php index 9d942c7ad859..36729d2ba624 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php @@ -111,4 +111,17 @@ trait SectionStorageTrait { return isset($this->getSections()[$delta]); } + /** + * Magic method: Implements a deep clone. + */ + public function __clone() { + $sections = $this->getSections(); + + foreach ($sections as $delta => $item) { + $sections[$delta] = clone $item; + } + + $this->setSections($sections); + } + } diff --git a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php index 1879977633e3..6c54d6c9e4ff 100644 --- a/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php +++ b/core/modules/layout_builder/tests/src/Kernel/SectionStorageTestBase.php @@ -137,6 +137,17 @@ abstract class SectionStorageTestBase extends EntityKernelTestBase { $this->assertSections($expected); } + /** + * Tests __clone(). + */ + public function testClone() { + $this->assertSame([], $this->sectionStorage->getSection(0)->getLayoutSettings()); + + $new_section_storage = clone $this->sectionStorage; + $new_section_storage->getSection(0)->setLayoutSettings(['asdf' => 'qwer']); + $this->assertSame([], $this->sectionStorage->getSection(0)->getLayoutSettings()); + } + /** * Asserts that the field list has the expected sections. *