Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Field / LayoutSectionItemList.php
diff --git a/web/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/web/core/modules/layout_builder/src/Field/LayoutSectionItemList.php
new file mode 100644 (file)
index 0000000..a2ceca1
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace Drupal\layout_builder\Field;
+
+use Drupal\Core\Field\FieldItemList;
+use Drupal\layout_builder\SectionListInterface;
+use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
+
+/**
+ * Defines a item list class for layout section fields.
+ *
+ * @internal
+ *
+ * @see \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem
+ */
+class LayoutSectionItemList extends FieldItemList implements SectionListInterface {
+
+  use SectionStorageTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getSections() {
+    $sections = [];
+    /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
+    foreach ($this->list as $delta => $item) {
+      $sections[$delta] = $item->section;
+    }
+    return $sections;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setSections(array $sections) {
+    $this->list = [];
+    $sections = array_values($sections);
+    /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
+    foreach ($sections as $section) {
+      $item = $this->appendItem();
+      $item->section = $section;
+    }
+
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function __wakeup() {
+    // Ensure the entity is updated with the latest value.
+    $this->getEntity()->set($this->getName(), $this->getValue());
+  }
+
+}