Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::setValue()
  2. 9 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::setValue()

Sets the data value.

Parameters

mixed|null $value: The value to set in the format as documented for the data type or NULL to unset the data value.

bool $notify: (optional) Whether to notify the parent object of the change. Defaults to TRUE. If a property is updated from a parent object, set it to FALSE to avoid being notified again.

Throws

\InvalidArgumentException If the value input is inappropriate.

\Drupal\Core\TypedData\Exception\ReadOnlyException If the data is read-only.

Overrides TypedDataInterface::setValue

3 calls to FieldItemList::setValue()
FieldItemList::applyDefaultValue in core/lib/Drupal/Core/Field/FieldItemList.php
Applies the default value.
FieldItemList::generateSampleItems in core/lib/Drupal/Core/Field/FieldItemList.php
Populates a specified number of field items with valid sample data.
ModerationStateFieldItemList::setValue in core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
Sets the data value.

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 101

Class

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

Namespace

Drupal\Core\Field

Code

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

  // Support passing in only the value of the first item, either as a literal
  // (value of the first property) or as an array of properties.
  if (isset($values) && (!is_array($values) || !empty($values) && !is_numeric(current(array_keys($values))))) {
    $values = [
      0 => $values,
    ];
  }
  parent::setValue($values, $notify);
}