From 02544cc395102a10d0f0e1ac6964e385aeeedea1 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Fri, 27 Apr 2018 08:47:50 +1000
Subject: [PATCH] Issue #2962248 by tim.plunkett: Layout Builder defaults
 should support third party settings

---
 .../src/DefaultsSectionStorageInterface.php   |  4 +-
 .../src/OverridesSectionStorageInterface.php  |  2 +-
 .../SectionStorage/DefaultsSectionStorage.php | 37 +++++++++++++++++++
 .../src/Unit/DefaultsSectionStorageTest.php   | 25 +++++++++++++
 4 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/core/modules/layout_builder/src/DefaultsSectionStorageInterface.php b/core/modules/layout_builder/src/DefaultsSectionStorageInterface.php
index 9397c6cea585..121d9c84eb63 100644
--- a/core/modules/layout_builder/src/DefaultsSectionStorageInterface.php
+++ b/core/modules/layout_builder/src/DefaultsSectionStorageInterface.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\layout_builder;
 
+use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
+
 /**
  * Defines an interface for an object that stores layout sections for defaults.
  *
@@ -10,7 +12,7 @@
  *   experimental modules and development releases of contributed modules.
  *   See https://www.drupal.org/core/experimental for more information.
  */
-interface DefaultsSectionStorageInterface extends SectionStorageInterface {
+interface DefaultsSectionStorageInterface extends SectionStorageInterface, ThirdPartySettingsInterface {
 
   /**
    * Determines if the defaults allow custom overrides.
diff --git a/core/modules/layout_builder/src/OverridesSectionStorageInterface.php b/core/modules/layout_builder/src/OverridesSectionStorageInterface.php
index 76e15b5a1aef..8d1787998b0a 100644
--- a/core/modules/layout_builder/src/OverridesSectionStorageInterface.php
+++ b/core/modules/layout_builder/src/OverridesSectionStorageInterface.php
@@ -15,7 +15,7 @@ interface OverridesSectionStorageInterface extends SectionStorageInterface {
   /**
    * Returns the corresponding defaults section storage for this override.
    *
-   * @return \Drupal\layout_builder\SectionStorageInterface
+   * @return \Drupal\layout_builder\DefaultsSectionStorageInterface
    *   The defaults section storage.
    *
    * @todo Determine if this method needs a parameter in
diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php
index c6ca9c663093..c21492a2ec9a 100644
--- a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php
+++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php
@@ -293,4 +293,41 @@ public function setOverridable($overridable = TRUE) {
     return $this;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function setThirdPartySetting($module, $key, $value) {
+    $this->getDisplay()->setThirdPartySetting($module, $key, $value);
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartySetting($module, $key, $default = NULL) {
+    return $this->getDisplay()->getThirdPartySetting($module, $key, $default);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartySettings($module) {
+    return $this->getDisplay()->getThirdPartySettings($module);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function unsetThirdPartySetting($module, $key) {
+    $this->getDisplay()->unsetThirdPartySetting($module, $key);
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartyProviders() {
+    return $this->getDisplay()->getThirdPartyProviders();
+  }
+
 }
diff --git a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php
index 995483fff9b6..7e5304dbbea4 100644
--- a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php
+++ b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php
@@ -9,6 +9,7 @@
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\FieldableEntityInterface;
 use Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator;
+use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
 use Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage;
 use Drupal\layout_builder\SectionStorage\SectionStorageDefinition;
 use Drupal\Tests\UnitTestCase;
@@ -53,6 +54,30 @@ protected function setUp() {
     $this->plugin = new DefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal());
   }
 
+  /**
+   * @covers ::getThirdPartySetting
+   * @covers ::setThirdPartySetting
+   */
+  public function testThirdPartySettings() {
+    // Set an initial value on the section list.
+    $section_list = $this->prophesize(LayoutEntityDisplayInterface::class);
+    $section_list->getThirdPartySetting('the_module', 'the_key', NULL)->willReturn('value 1');
+    $this->plugin->setSectionList($section_list->reveal());
+
+    // The plugin returns the initial value.
+    $this->assertSame('value 1', $this->plugin->getThirdPartySetting('the_module', 'the_key'));
+
+    // When the section list is updated, also update the result returned.
+    $section_list->setThirdPartySetting('the_module', 'the_key', 'value 2')->shouldBeCalled()->will(function ($args) {
+      $this->getThirdPartySetting('the_module', 'the_key', NULL)->willReturn($args[2]);
+    });
+
+    // Update the plugin value.
+    $this->plugin->setThirdPartySetting('the_module', 'the_key', 'value 2');
+    // Assert that the returned value matches.
+    $this->assertSame('value 2', $this->plugin->getThirdPartySetting('the_module', 'the_key'));
+  }
+
   /**
    * @covers ::extractIdFromRoute
    *
-- 
GitLab