e86df9daf840173346ee4fcbe86ea6e8182f19e0
[yaffs-website] / web / core / modules / layout_builder / src / Entity / LayoutBuilderEntityViewDisplayStorage.php
1 <?php
2
3 namespace Drupal\layout_builder\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityStorage;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\layout_builder\Section;
8 use Drupal\layout_builder\SectionComponent;
9
10 /**
11  * Provides storage for entity view display entities that have layouts.
12  *
13  * @internal
14  *   Layout Builder is currently experimental and should only be leveraged by
15  *   experimental modules and development releases of contributed modules.
16  *   See https://www.drupal.org/core/experimental for more information.
17  */
18 class LayoutBuilderEntityViewDisplayStorage extends ConfigEntityStorage {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function mapToStorageRecord(EntityInterface $entity) {
24     $record = parent::mapToStorageRecord($entity);
25
26     if (!empty($record['third_party_settings']['layout_builder']['sections'])) {
27       $record['third_party_settings']['layout_builder']['sections'] = array_map(function (Section $section) {
28         return $section->toArray();
29       }, $record['third_party_settings']['layout_builder']['sections']);
30     }
31     return $record;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function mapFromStorageRecords(array $records) {
38     foreach ($records as $id => &$record) {
39       if (!empty($record['third_party_settings']['layout_builder']['sections'])) {
40         $sections = &$record['third_party_settings']['layout_builder']['sections'];
41         foreach ($sections as $section_delta => $section) {
42           $sections[$section_delta] = new Section(
43             $section['layout_id'],
44             $section['layout_settings'],
45             array_map(function (array $component) {
46               return (new SectionComponent(
47                 $component['uuid'],
48                 $component['region'],
49                 $component['configuration'],
50                 $component['additional']
51               ))->setWeight($component['weight']);
52             }, $section['components'])
53           );
54         }
55       }
56     }
57     return parent::mapFromStorageRecords($records);
58   }
59
60 }