class LayoutSectionItemList

Same name and namespace in other branches
  1. 11.x core/modules/layout_builder/src/Field/LayoutSectionItemList.php \Drupal\layout_builder\Field\LayoutSectionItemList
  2. 10 core/modules/layout_builder/src/Field/LayoutSectionItemList.php \Drupal\layout_builder\Field\LayoutSectionItemList
  3. 8.9.x core/modules/layout_builder/src/Field/LayoutSectionItemList.php \Drupal\layout_builder\Field\LayoutSectionItemList

Defines an item list class for layout section fields.

@internal Plugin classes are internal.

Hierarchy

Expanded class hierarchy of LayoutSectionItemList

See also

\Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem

File

core/modules/layout_builder/src/Field/LayoutSectionItemList.php, line 21

Namespace

Drupal\layout_builder\Field
View source
class LayoutSectionItemList extends FieldItemList implements SectionListInterface {
  use SectionListTrait;
  
  /**
   * Numerically indexed array of field items.
   *
   * @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem[]
   */
  protected $list = [];
  
  /**
   * {@inheritdoc}
   */
  public function getSections() {
    $sections = [];
    foreach ($this->list as $delta => $item) {
      $sections[$delta] = $item->section;
    }
    return $sections;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function setSections(array $sections) {
    $this->list = [];
    $sections = array_values($sections);
    /** @var \Drupal\layout_builder\Plugin\Field\FieldType\LayoutSectionItem $item */
    foreach ($sections as $section) {
      $item = $this->appendItem();
      $item->section = $section;
    }
    return $this;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    $entity = parent::getEntity();
    // Ensure the entity is updated with the latest value.
    $entity->set($this->getName(), $this->getValue());
    return $entity;
  }
  
  /**
   * {@inheritdoc}
   */
  public function preSave() {
    parent::preSave();
    // Loop through each section and reconstruct it to ensure that all default
    // values are present.
    foreach ($this->list as $item) {
      $item->section = Section::fromArray($item->section
        ->toArray());
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function equals(FieldItemListInterface $list_to_compare) {
    if (!$list_to_compare instanceof LayoutSectionItemList) {
      return FALSE;
    }
    // Convert arrays of section objects to array values for comparison.
    $convert = function (LayoutSectionItemList $list) {
      return array_map(function (Section $section) {
        return $section->toArray();
      }, $list->getSections());
    };
    return $convert($this) === $convert($list_to_compare);
  }
  
  /**
   * Overrides \Drupal\Core\Field\FieldItemListInterface::defaultAccess().
   *
   * @ingroup layout_builder_access
   */
  public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
    // @todo Allow access in https://www.drupal.org/node/2942975.
    return AccessResult::forbidden();
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.