LayoutSectionItemList.php 2.73 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\layout_builder\Field;

use Drupal\Core\Field\FieldItemList;
6 7
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionStorageInterface;
8 9 10 11 12 13 14 15

/**
 * Defines a item list class for layout section fields.
 *
 * @internal
 *
 * @see \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem
 */
16
class LayoutSectionItemList extends FieldItemList implements SectionStorageInterface {
17 18 19 20

  /**
   * {@inheritdoc}
   */
21 22 23 24 25 26 27 28
  public function insertSection($delta, Section $section) {
    if ($this->get($delta)) {
      /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
      $item = $this->createItem($delta);
      $item->section = $section;

      $start = array_slice($this->list, 0, $delta);
      $end = array_slice($this->list, $delta);
29 30 31
      $this->list = array_merge($start, [$item], $end);
    }
    else {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
      $this->appendSection($section);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function appendSection(Section $section) {
    $this->appendItem()->section = $section;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getSections() {
    $sections = [];
    /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
    foreach ($this->list as $delta => $item) {
      $sections[$delta] = $item->section;
53
    }
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    return $sections;
  }

  /**
   * {@inheritdoc}
   */
  public function getSection($delta) {
    /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
    if (!$item = $this->get($delta)) {
      throw new \OutOfBoundsException(sprintf('Invalid delta "%s" for the "%s" entity', $delta, $this->getEntity()->label()));
    }

    return $item->section;
  }

  /**
   * {@inheritdoc}
   */
  public function removeSection($delta) {
    $this->removeItem($delta);
    return $this;
75 76
  }

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  /**
   * {@inheritdoc}
   */
  public function getStorageType() {
    return 'overrides';
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageId() {
    $entity = $this->getEntity();
    return $entity->getEntityTypeId() . ':' . $entity->id();
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->getEntity()->label();
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    return $this->getEntity()->save();
  }

  /**
   * {@inheritdoc}
   */
  public function getCanonicalUrl() {
    return $this->getEntity()->toUrl('canonical');
  }

  /**
   * {@inheritdoc}
   */
  public function getLayoutBuilderUrl() {
    return $this->getEntity()->toUrl('layout-builder');
  }

  /**
   * {@inheritdoc}
   */
  public function __wakeup() {
    // Ensure the entity is updated with the latest value.
    $this->getEntity()->set($this->getName(), $this->getValue());
  }

128
}