Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php \Drupal\Core\TypedData\ComputedItemListTrait
  2. 9 core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php \Drupal\Core\TypedData\ComputedItemListTrait

Provides common functionality for computed item lists.

Hierarchy

See also

\Drupal\Core\TypedData\ListInterface

\Drupal\Core\TypedData\Plugin\DataType\ItemList

\Drupal\Core\Field\FieldItemListInterface

\Drupal\Core\Field\FieldItemList

Related topics

7 files declare their use of ComputedItemListTrait
ComputedReferenceTestFieldItemList.php in core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedReferenceTestFieldItemList.php
ComputedTestBundleFieldItemList.php in core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestBundleFieldItemList.php
ComputedTestCacheableIntegerItemList.php in core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestCacheableIntegerItemList.php
ComputedTestCacheableStringItemList.php in core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestCacheableStringItemList.php
ComputedTestFieldItemList.php in core/modules/system/tests/modules/entity_test/src/Plugin/Field/ComputedTestFieldItemList.php

... See full list

File

core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php, line 15

Namespace

Drupal\Core\TypedData
View source
trait ComputedItemListTrait {

  /**
   * Whether the values have already been computed or not.
   *
   * @var bool
   */
  protected $valueComputed = FALSE;

  /**
   * Computes the values for an item list.
   */
  protected abstract function computeValue();

  /**
   * Ensures that values are only computed once.
   */
  protected function ensureComputedValue() {
    if ($this->valueComputed === FALSE) {
      $this
        ->computeValue();
      $this->valueComputed = TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    $this
      ->ensureComputedValue();
    return parent::getValue();
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($values, $notify = TRUE) {
    parent::setValue($values, $notify);

    // Make sure that subsequent getter calls do not try to compute the values
    // again.
    $this->valueComputed = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getString() {
    $this
      ->ensureComputedValue();
    return parent::getString();
  }

  /**
   * {@inheritdoc}
   */
  public function get($index) {
    if (!is_numeric($index)) {
      throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
    }

    // Unlike the base implementation of
    // \Drupal\Core\TypedData\ListInterface::get(), we do not add an empty item
    // automatically because computed item lists need to behave like
    // non-computed ones. For example, calling isEmpty() on a computed item list
    // should return TRUE when the values were computed and the item list is
    // truly empty.
    // @see \Drupal\Core\TypedData\Plugin\DataType\ItemList::get().
    $this
      ->ensureComputedValue();
    return $this->list[$index] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function set($index, $value) {
    $this
      ->ensureComputedValue();
    return parent::set($index, $value);
  }

  /**
   * {@inheritdoc}
   */
  public function appendItem($value = NULL) {
    $this
      ->ensureComputedValue();
    return parent::appendItem($value);
  }

  /**
   * {@inheritdoc}
   */
  public function removeItem($index) {
    $this
      ->ensureComputedValue();
    return parent::removeItem($index);
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $this
      ->ensureComputedValue();
    return parent::isEmpty();
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function offsetExists($offset) {
    $this
      ->ensureComputedValue();
    return parent::offsetExists($offset);
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function getIterator() {
    $this
      ->ensureComputedValue();
    return parent::getIterator();
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function count() {
    $this
      ->ensureComputedValue();
    return parent::count();
  }

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {

    // Default values do not make sense for computed item lists. However, this
    // method can be overridden if needed.
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComputedItemListTrait::$valueComputed protected property Whether the values have already been computed or not.
ComputedItemListTrait::appendItem public function
ComputedItemListTrait::applyDefaultValue public function
ComputedItemListTrait::computeValue abstract protected function Computes the values for an item list. 6
ComputedItemListTrait::count public function
ComputedItemListTrait::ensureComputedValue protected function Ensures that values are only computed once.
ComputedItemListTrait::get public function
ComputedItemListTrait::getIterator public function
ComputedItemListTrait::getString public function
ComputedItemListTrait::getValue public function
ComputedItemListTrait::isEmpty public function
ComputedItemListTrait::offsetExists public function
ComputedItemListTrait::removeItem public function
ComputedItemListTrait::set public function
ComputedItemListTrait::setValue public function 1