Issue #2381777 by yched: Unify setValue() implementations in ItemList & FieldItemList

8.0.x
Alex Pott 2014-12-23 15:39:13 +01:00
parent ff41283c76
commit 37cdcf8d97
2 changed files with 6 additions and 31 deletions

View File

@ -124,37 +124,12 @@ class FieldItemList extends ItemList implements FieldItemListInterface {
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
if (!isset($values) || $values === array()) {
$this->list = array();
}
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);
}
}
}
// Notify the parent of any changes.
if ($notify && isset($this->parent)) {
$this->parent->onChange($this->name);
// Support passing in only the value of the first item, either as a litteral
// (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 = array(0 => $values);
}
parent::setValue($values, $notify);
}
/**

View File

@ -79,7 +79,7 @@ class ItemList extends TypedData implements \IteratorAggregate, ListInterface {
$this->list[$delta] = $this->createItem($delta, $value);
}
else {
$this->list[$delta]->setValue($value);
$this->list[$delta]->setValue($value, FALSE);
}
}
}