public function Field::setValue

Overrides \Drupal\Core\TypedData\ItemList::setValue().

Overrides ItemList::setValue

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/Field.php, line 62
Contains \Drupal\Core\Entity\Field\Type\Field.

Class

Field
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Entity\Field\Type

Code

public function setValue($values, $notify = TRUE) {

  // Notify the parent of any changes to be made.
  if ($notify && isset($this->parent)) {
    $this->parent
      ->onChange($this->name);
  }
  if (!isset($values) || $values === array()) {
    $this->list = $values;
  }
  else {

    // Support passing in only the value of the first item.
    if (!is_array($values) || !is_numeric(current(array_keys($values)))) {
      $values = array(
        0 => $values,
      );
    }

    // Clear the values of properties for which no value has been passed.
    if (isset($this->list)) {
      $this->list = array_intersect_key($this->list, $values);
    }

    // Set the values.
    foreach ($values as $delta => $value) {
      if (!is_numeric($delta)) {
        throw new \InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
      }
      elseif (!isset($this->list[$delta])) {
        $this->list[$delta] = $this
          ->createItem($delta, $value);
      }
      else {
        $this->list[$delta]
          ->setValue($value, FALSE);
      }
    }
  }
}