Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / Field / LayoutSectionItemList.php
1 <?php
2
3 namespace Drupal\layout_builder\Field;
4
5 use Drupal\Core\Field\FieldItemList;
6 use Drupal\layout_builder\SectionListInterface;
7 use Drupal\layout_builder\SectionStorage\SectionStorageTrait;
8
9 /**
10  * Defines a item list class for layout section fields.
11  *
12  * @internal
13  *
14  * @see \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem
15  */
16 class LayoutSectionItemList extends FieldItemList implements SectionListInterface {
17
18   use SectionStorageTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getSections() {
24     $sections = [];
25     /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
26     foreach ($this->list as $delta => $item) {
27       $sections[$delta] = $item->section;
28     }
29     return $sections;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setSections(array $sections) {
36     $this->list = [];
37     $sections = array_values($sections);
38     /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
39     foreach ($sections as $section) {
40       $item = $this->appendItem();
41       $item->section = $section;
42     }
43
44     return $this;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function getEntity() {
51     $entity = parent::getEntity();
52
53     // Ensure the entity is updated with the latest value.
54     $entity->set($this->getName(), $this->getValue());
55     return $entity;
56   }
57
58 }