diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index f6d44ba..427fe68 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -440,7 +440,7 @@ public function __set($name, $value) { */ public function __isset($name) { if ($this->getPropertyDefinition($name)) { - return (bool) count($this->get($name)); + return $this->get($name)->getValue() !== NULL; } else { return isset($this->values[$name]); diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index 5daa81f..06e3564 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -38,13 +38,6 @@ class Field extends ContextAwareTypedData implements IteratorAggregate, FieldInt protected $list = array(); /** - * Flag to indicate if this field has been set. - * - * @var bool - */ - protected $isset = FALSE; - - /** * Overrides ContextAwareTypedData::__construct(). */ public function __construct(array $definition, $name = NULL, ContextAwareInterface $parent = NULL) { @@ -60,16 +53,18 @@ public function __construct(array $definition, $name = NULL, ContextAwareInterfa * Implements TypedDataInterface::getValue(). */ public function getValue() { - $values = array(); - foreach ($this->list as $delta => $item) { - if (!$item->isEmpty()) { - $values[$delta] = $item->getValue(); - } - else { - $values[$delta] = NULL; + if (isset($this->list)) { + $values = array(); + foreach ($this->list as $delta => $item) { + if (!$item->isEmpty()) { + $values[$delta] = $item->getValue(); + } + else { + $values[$delta] = NULL; + } } + return $values; } - return $values; } /** @@ -79,7 +74,6 @@ public function getValue() { * An array of values of the field items. */ public function setValue($values) { - $this->isset = TRUE; if (isset($values) && $values !== array()) { // Support passing in only the value of the first item. if (!is_array($values) || !is_numeric(current(array_keys($values)))) { @@ -87,7 +81,9 @@ public function setValue($values) { } // Clear the values of properties for which no value has been passed. - $this->list = array_intersect_key($this->list, $values); + if (isset($this->list)) { + $this->list = array_intersect_key($this->list, $values); + } // Set the values. foreach ($values as $delta => $value) { @@ -102,17 +98,12 @@ public function setValue($values) { } } } - else { + elseif ($values === array()) { $this->list = array(); } - } - - /** - * Mark this field as not set. - */ - public function unsetValue() { - $this->list = array(); - $this->isset = FALSE; + else { + $this->list = NULL; + } } /** @@ -122,10 +113,12 @@ public function unsetValue() { */ public function getString() { $strings = array(); - foreach ($this->list() as $item) { - $strings[] = $item->getString(); + if (isset($this->list)) { + foreach ($this->list() as $item) { + $strings[] = $item->getString(); + } + return implode(', ', array_filter($strings)); } - return implode(', ', array_filter($strings)); } /** @@ -139,14 +132,16 @@ public function validate() { * Implements ArrayAccess::offsetExists(). */ public function offsetExists($offset) { - return array_key_exists($offset, $this->list); + return isset($this->list) && array_key_exists($offset, $this->list); } /** * Implements ArrayAccess::offsetUnset(). */ public function offsetUnset($offset) { - unset($this->list[$offset]); + if (isset($this->list)) { + unset($this->list[$offset]); + } } /** @@ -204,14 +199,17 @@ public function offsetSet($offset, $value) { * Implements IteratorAggregate::getIterator(). */ public function getIterator() { - return new ArrayIterator($this->list); + if (isset($this->list)) { + return new ArrayIterator($this->list); + } + return new ArrayIterator(array()); } /** * Implements Countable::count(). */ public function count() { - return count($this->list); + return isset($this->list) ? count($this->list) : 0; } /** @@ -247,14 +245,13 @@ public function get($property_name) { */ public function __set($property_name, $value) { $this->offsetGet(0)->__set($property_name, $value); - $this->isset = TRUE; } /** * Delegate. */ public function __isset($property_name) { - return $this->isset && $this->offsetGet(0)->__isset($property_name); + return $this->offsetGet(0)->__isset($property_name); } /** @@ -268,31 +265,26 @@ public function __unset($property_name) { * Implements ListInterface::isEmpty(). */ public function isEmpty() { - foreach ($this->list as $item) { - if (!$item->isEmpty()) { - return FALSE; + if (isset($this->list)) { + foreach ($this->list as $item) { + if (!$item->isEmpty()) { + return FALSE; + } } } return TRUE; } /** - * Determines if this field has been set. - * - * @return bool - */ - public function valueIsSet() { - return $this->isset; - } - - /** * Implements a deep clone. */ public function __clone() { - foreach ($this->list as $delta => $property) { - $this->list[$delta] = clone $property; - if ($property instanceof ContextAwareInterface) { - $this->list[$delta]->setContext($delta, $this); + if (isset($this->list)) { + foreach ($this->list as $delta => $property) { + $this->list[$delta] = clone $property; + if ($property instanceof ContextAwareInterface) { + $this->list[$delta]->setContext($delta, $this); + } } } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index c69b90f..56f3873 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -226,7 +226,7 @@ public function testReadWrite() { // Test removing all list items by setting it to NULL. $entity->name = NULL; $this->assertIdentical(count($entity->name), 0, 'Name field contains no items.'); - $this->assertIdentical($entity->name->getValue(), array(), 'Name field value is an empty array.'); + $this->assertNull($entity->name->getValue(), 'Name field value is NULL.'); // Test get and set field values. $entity->name = 'foo';