c419060c45b8ac068a63ae13f5cc118a42a2302d
[yaffs-website] / web / core / modules / layout_builder / src / Plugin / SectionStorage / SectionStorageBase.php
1 <?php
2
3 namespace Drupal\layout_builder\Plugin\SectionStorage;
4
5 use Drupal\Core\Plugin\PluginBase;
6 use Drupal\layout_builder\Routing\LayoutBuilderRoutesTrait;
7 use Drupal\layout_builder\Section;
8 use Drupal\layout_builder\SectionListInterface;
9 use Drupal\layout_builder\SectionStorageInterface;
10
11 /**
12  * Provides a base class for Section Storage types.
13  *
14  * @internal
15  *   Layout Builder is currently experimental and should only be leveraged by
16  *   experimental modules and development releases of contributed modules.
17  *   See https://www.drupal.org/core/experimental for more information.
18  */
19 abstract class SectionStorageBase extends PluginBase implements SectionStorageInterface {
20
21   use LayoutBuilderRoutesTrait;
22
23   /**
24    * The section storage instance.
25    *
26    * @var \Drupal\layout_builder\SectionListInterface|null
27    */
28   protected $sectionList;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function setSectionList(SectionListInterface $section_list) {
34     $this->sectionList = $section_list;
35     return $this;
36   }
37
38   /**
39    * Gets the section list.
40    *
41    * @return \Drupal\layout_builder\SectionListInterface
42    *   The section list.
43    *
44    * @throws \RuntimeException
45    *   Thrown if ::setSectionList() is not called first.
46    */
47   protected function getSectionList() {
48     if (!$this->sectionList) {
49       throw new \RuntimeException(sprintf('%s::setSectionList() must be called first', static::class));
50     }
51     return $this->sectionList;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getStorageType() {
58     return $this->getPluginId();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function count() {
65     return $this->getSectionList()->count();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getSections() {
72     return $this->getSectionList()->getSections();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getSection($delta) {
79     return $this->getSectionList()->getSection($delta);
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function appendSection(Section $section) {
86     $this->getSectionList()->appendSection($section);
87     return $this;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function insertSection($delta, Section $section) {
94     $this->getSectionList()->insertSection($delta, $section);
95     return $this;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function removeSection($delta) {
102     $this->getSectionList()->removeSection($delta);
103     return $this;
104   }
105
106 }