X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FSectionStorage%2FSectionStorageTrait.php;fp=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FSectionStorage%2FSectionStorageTrait.php;h=9d942c7ad85982898e0480f3f7cac4d075b2f83e;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/web/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php b/web/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php new file mode 100644 index 000000000..9d942c7ad --- /dev/null +++ b/web/core/modules/layout_builder/src/SectionStorage/SectionStorageTrait.php @@ -0,0 +1,114 @@ +getSections()); + } + + /** + * {@inheritdoc} + */ + public function getSection($delta) { + if (!$this->hasSection($delta)) { + throw new \OutOfBoundsException(sprintf('Invalid delta "%s"', $delta)); + } + + return $this->getSections()[$delta]; + } + + /** + * Sets the section for the given delta on the display. + * + * @param int $delta + * The delta of the section. + * @param \Drupal\layout_builder\Section $section + * The layout section. + * + * @return $this + */ + protected function setSection($delta, Section $section) { + $sections = $this->getSections(); + $sections[$delta] = $section; + $this->setSections($sections); + return $this; + } + + /** + * {@inheritdoc} + */ + public function appendSection(Section $section) { + $delta = $this->count(); + + $this->setSection($delta, $section); + return $this; + } + + /** + * {@inheritdoc} + */ + public function insertSection($delta, Section $section) { + if ($this->hasSection($delta)) { + // @todo Use https://www.drupal.org/node/66183 once resolved. + $start = array_slice($this->getSections(), 0, $delta); + $end = array_slice($this->getSections(), $delta); + $this->setSections(array_merge($start, [$section], $end)); + } + else { + $this->appendSection($section); + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function removeSection($delta) { + $sections = $this->getSections(); + unset($sections[$delta]); + $this->setSections($sections); + return $this; + } + + /** + * Indicates if there is a section at the specified delta. + * + * @param int $delta + * The delta of the section. + * + * @return bool + * TRUE if there is a section for this delta, FALSE otherwise. + */ + protected function hasSection($delta) { + return isset($this->getSections()[$delta]); + } + +}