diff core/modules/layout_builder/src/Section.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
line wrap: on
line diff
--- a/core/modules/layout_builder/src/Section.php	Thu Feb 28 13:21:36 2019 +0000
+++ b/core/modules/layout_builder/src/Section.php	Thu May 09 15:33:08 2019 +0100
@@ -2,6 +2,8 @@
 
 namespace Drupal\layout_builder;
 
+use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
+
 /**
  * Provides a domain object for layout sections.
  *
@@ -11,18 +13,10 @@
  * - An array of settings for the layout plugin.
  * - An array of components that can be rendered in the section.
  *
- * @internal
- *   Layout Builder is currently experimental and should only be leveraged by
- *   experimental modules and development releases of contributed modules.
- *   See https://www.drupal.org/core/experimental for more information.
- *
  * @see \Drupal\Core\Layout\LayoutDefinition
  * @see \Drupal\layout_builder\SectionComponent
- *
- * @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 {
 
   /**
    * The layout plugin ID.
@@ -46,6 +40,15 @@
   protected $components = [];
 
   /**
+   * Third party settings.
+   *
+   * An array of key/value pairs keyed by provider.
+   *
+   * @var array[]
+   */
+  protected $thirdPartySettings = [];
+
+  /**
    * Constructs a new Section.
    *
    * @param string $layout_id
@@ -54,13 +57,16 @@
    *   (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->thirdPartySettings = $third_party_settings;
   }
 
   /**
@@ -240,7 +246,7 @@
    * @return \Drupal\layout_builder\SectionComponent[]
    *   An array of components in the specified region, sorted by weight.
    */
-  protected function getComponentsByRegion($region) {
+  public function getComponentsByRegion($region) {
     $components = array_filter($this->getComponents(), function (SectionComponent $component) use ($region) {
       return $component->getRegion() === $region;
     });
@@ -334,6 +340,7 @@
       'components' => array_map(function (SectionComponent $component) {
         return $component->toArray();
       }, $this->getComponents()),
+      'third_party_settings' => $this->thirdPartySettings,
     ];
   }
 
@@ -349,10 +356,18 @@
    *   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']
     );
   }
 
@@ -365,4 +380,46 @@
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartySetting($provider, $key, $default = NULL) {
+    return isset($this->thirdPartySettings[$provider][$key]) ? $this->thirdPartySettings[$provider][$key] : $default;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartySettings($provider) {
+    return isset($this->thirdPartySettings[$provider]) ? $this->thirdPartySettings[$provider] : [];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setThirdPartySetting($provider, $key, $value) {
+    $this->thirdPartySettings[$provider][$key] = $value;
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function unsetThirdPartySetting($provider, $key) {
+    unset($this->thirdPartySettings[$provider][$key]);
+    // If the third party is no longer storing any information, completely
+    // remove the array holding the settings for this provider.
+    if (empty($this->thirdPartySettings[$provider])) {
+      unset($this->thirdPartySettings[$provider]);
+    }
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getThirdPartyProviders() {
+    return array_keys($this->thirdPartySettings);
+  }
+
 }