function ItemList::set

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php \Drupal\Core\TypedData\Plugin\DataType\ItemList::set()
  2. 8.9.x core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php \Drupal\Core\TypedData\Plugin\DataType\ItemList::set()
  3. 10 core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php \Drupal\Core\TypedData\Plugin\DataType\ItemList::set()

Overrides ListInterface::set

1 call to ItemList::set()
ItemList::offsetSet in core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php

File

core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php, line 111

Class

ItemList
A generic list class.

Namespace

Drupal\Core\TypedData\Plugin\DataType

Code

public function set($index, $value) {
    if (!is_numeric($index)) {
        throw new \InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
    }
    // Ensure indexes stay sequential. We allow assigning an item at an existing
    // index, or at the next index available.
    if ($index < 0 || $index > count($this->list)) {
        throw new \InvalidArgumentException('Unable to set a value to a non-subsequent delta in a list.');
    }
    // Support setting values via typed data objects.
    if ($value instanceof TypedDataInterface) {
        $value = $value->getValue();
    }
    // If needed, create the item at the next position.
    $item = $this->list[$index] ?? $this->appendItem();
    $item->setValue($value);
    return $this;
}

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