diff --git a/core/modules/layout_builder/config/schema/layout_builder.schema.yml b/core/modules/layout_builder/config/schema/layout_builder.schema.yml index 7faad99c24..329e5bb590 100644 --- a/core/modules/layout_builder/config/schema/layout_builder.schema.yml +++ b/core/modules/layout_builder/config/schema/layout_builder.schema.yml @@ -28,6 +28,11 @@ layout_builder.section: label: 'Components' sequence: type: layout_builder.component + third_party_settings: + type: sequence + label: 'Third party settings' + sequence: + type: '[%parent.%parent.%type].third_party.[%key]' layout_builder.component: type: mapping @@ -47,6 +52,11 @@ layout_builder.component: additional: type: ignore label: 'Additional data' + third_party_settings: + type: sequence + label: 'Third party settings' + sequence: + type: '[%parent.%parent.%type].third_party.[%key]' inline_block: type: block_settings diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 1ec0b8af10..73173c4087 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -2,6 +2,8 @@ namespace Drupal\layout_builder; +use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; + /** * Provides a domain object for layout sections. * @@ -22,7 +24,9 @@ * @todo Determine whether an interface will be provided for this in * https://www.drupal.org/project/drupal/issues/2930334. */ -class Section { +class Section implements ThirdPartySettingsInterface { + + use ThirdPartySettingsTrait; /** * The layout plugin ID. @@ -54,13 +58,16 @@ class Section { * (optional) The layout plugin settings. * @param \Drupal\layout_builder\SectionComponent[] $components * (optional) The components. + * @param array $third_party_settings + * (optional) Any third party settings. */ - public function __construct($layout_id, array $layout_settings = [], array $components = []) { + public function __construct($layout_id, array $layout_settings = [], array $components = [], array $third_party_settings = []) { $this->layoutId = $layout_id; $this->layoutSettings = $layout_settings; foreach ($components as $component) { $this->setComponent($component); } + $this->third_party_settings = $third_party_settings; } /** @@ -334,6 +341,7 @@ public function toArray() { 'components' => array_map(function (SectionComponent $component) { return $component->toArray(); }, $this->getComponents()), + 'third_party_settings' => $this->third_party_settings, ]; } @@ -349,10 +357,18 @@ public function toArray() { * The section object. */ public static function fromArray(array $section) { + // Ensure expected array keys are present. + $section += [ + 'layout_id' => '', + 'layout_settings' => [], + 'components' => [], + 'third_party_settings' => [], + ]; return new static( $section['layout_id'], $section['layout_settings'], - array_map([SectionComponent::class, 'fromArray'], $section['components']) + array_map([SectionComponent::class, 'fromArray'], $section['components']), + $section['third_party_settings'] ); } diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 4937f7fbcd..25a06719c7 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -3,6 +3,7 @@ namespace Drupal\layout_builder; use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; @@ -28,7 +29,9 @@ * @todo Determine whether an interface will be provided for this in * https://www.drupal.org/project/drupal/issues/2930334. */ -class SectionComponent { +class SectionComponent implements ThirdPartySettingsInterface { + + use ThirdPartySettingsTrait; /** * The UUID of the component. @@ -76,12 +79,15 @@ class SectionComponent { * The plugin configuration. * @param mixed[] $additional * An additional values. + * @param array $third_party_settings + * (optional) Any third party settings. */ - public function __construct($uuid, $region, array $configuration = [], array $additional = []) { + public function __construct($uuid, $region, array $configuration = [], array $additional = [], array $third_party_settings = []) { $this->uuid = $uuid; $this->region = $region; $this->configuration = $configuration; $this->additional = $additional; + $this->third_party_settings = $third_party_settings; } /** @@ -304,6 +310,7 @@ public function toArray() { 'configuration' => $this->getConfiguration(), 'additional' => $this->additional, 'weight' => $this->getWeight(), + 'third_party_settings' => $this->third_party_settings, ]; } @@ -319,11 +326,20 @@ public function toArray() { * The section component object. */ public static function fromArray(array $component) { + // Ensure expected array keys are present. + $component += [ + 'uuid' => '', + 'region' => [], + 'configuration' => [], + 'additional' => [], + 'third_party_settings' => [], + ]; return (new static( $component['uuid'], $component['region'], $component['configuration'], - $component['additional'] + $component['additional'], + $component['third_party_settings'] ))->setWeight($component['weight']); } diff --git a/core/modules/layout_builder/tests/fixtures/update/layout-builder-enable.php b/core/modules/layout_builder/tests/fixtures/update/layout-builder-enable.php index fc69baae33..d770c6fd65 100644 --- a/core/modules/layout_builder/tests/fixtures/update/layout-builder-enable.php +++ b/core/modules/layout_builder/tests/fixtures/update/layout-builder-enable.php @@ -30,8 +30,10 @@ ], 'additional' => [], 'weight' => 0, + 'third_party_settings' => [], ], ], + 'third_party_settings' => [], ]; $connection->update('config') ->fields([ diff --git a/core/modules/layout_builder/tests/src/Functional/Update/LayoutBuilderEnableUpdatePathTest.php b/core/modules/layout_builder/tests/src/Functional/Update/LayoutBuilderEnableUpdatePathTest.php index 5577539359..5cc1d38d2a 100644 --- a/core/modules/layout_builder/tests/src/Functional/Update/LayoutBuilderEnableUpdatePathTest.php +++ b/core/modules/layout_builder/tests/src/Functional/Update/LayoutBuilderEnableUpdatePathTest.php @@ -45,8 +45,10 @@ public function testRunUpdates() { ], 'additional' => [], 'weight' => 0, + 'third_party_settings' => [], ], ], + 'third_party_settings' => [], ], ], ]; diff --git a/core/modules/layout_builder/tests/src/Unit/SectionComponentTest.php b/core/modules/layout_builder/tests/src/Unit/SectionComponentTest.php index 48a24778c1..c86dda230c 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionComponentTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionComponentTest.php @@ -20,6 +20,8 @@ */ class SectionComponentTest extends UnitTestCase { + use ThirdPartySettingsTestTrait; + /** * @covers ::toRenderArray */ @@ -68,4 +70,11 @@ public function testToRenderArray() { $this->assertEquals($expected, $result); } + /** + * {@inheritdoc} + */ + private function getThirdPartySettingsObject() { + return new SectionComponent('', '', [], [], $this->initialThirdPartySettings); + } + } diff --git a/core/modules/layout_builder/tests/src/Unit/SectionTest.php b/core/modules/layout_builder/tests/src/Unit/SectionTest.php index eff239507d..86aba110ec 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionTest.php @@ -12,6 +12,8 @@ */ class SectionTest extends UnitTestCase { + use ThirdPartySettingsTestTrait; + /** * The section object to test. * @@ -25,11 +27,16 @@ class SectionTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->section = new Section('layout_onecol', [], [ - 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), - ]); + $this->section = new Section( + 'layout_onecol', + [], + [ + 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), + ], + $this->initialThirdPartySettings + ); } /** @@ -179,4 +186,11 @@ protected function assertComponents(array $expected, Section $section) { $this->assertSame(array_keys($expected), array_keys($result)); } + /** + * {@inheritdoc} + */ + private function getThirdPartySettingsObject() { + return $this->section; + } + }