diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index 1c2f4af8742b..e7a596045c6e 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -68,16 +68,12 @@ class Field extends TypedData implements IteratorAggregate, FieldInterface { * An array of values of the field items. */ public function setValue($values) { - // Support passing in only the value of the first item. - if (!is_array($values) || (!empty($values) && !is_numeric(current(array_keys($values))))) { - $values = array(0 => $values); - } + 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)))) { + $values = array(0 => $values); + } - if (!is_array($values)) { - throw new InvalidArgumentException("An entity field requires a numerically indexed array of items as value."); - } - - if (!empty($values)) { if (!is_array($values)) { throw new InvalidArgumentException("An entity field requires a numerically indexed array of items as value."); } 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 fa443f65013e..a152bfa43e9c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -210,6 +210,16 @@ class EntityFieldTest extends WebTestBase { $this->assertEqual(count(iterator_to_array($entity->name->getIterator())), count($entity->name), 'Count matches iterator count.'); $this->assertTrue($entity->name->getValue() === array(0 => NULL), 'Name field value contains a NULL value.'); + // Test removing all list items by assigning an empty array. + $entity->name = array(); + $this->assertIdentical(count($entity->name), 0, 'Name field contains no items.'); + $this->assertIdentical($entity->name->getValue(), array(), 'Name field value is an empty array.'); + + // 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.'); + // Test get and set field values. $entity->name = 'foo'; $this->assertEqual($entity->name[0]->getPropertyValues(), array('value' => 'foo'), 'Field value has been retrieved via getPropertyValue()'); @@ -230,6 +240,15 @@ class EntityFieldTest extends WebTestBase { )); $this->assertNotNull($entity->user_id->value, 'User id is not NULL'); $this->assertIdentical($entity->user_id->value, 0, 'User id has been set to 0'); + + // Test setting the ID with the value only. + $entity = entity_create('entity_test', array( + 'name' => $name_item, + 'user_id' => 0, + 'field_test_text' => $text_item, + )); + $this->assertNotNull($entity->user_id->value, 'User id is not NULL'); + $this->assertIdentical($entity->user_id->value, 0, 'User id has been set to 0'); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php index 9c38fdaec192..e0b25fb759b4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php @@ -171,15 +171,6 @@ class EntityTranslationTest extends WebTestBase { $entity->getTranslation($this->langcodes[1], FALSE)->set($field_name, array(0 => array('value' => 'default value2'))); // Get the value. $this->assertEqual($entity->get($field_name)->value, 'default value2', 'Untranslated value set into a translation in non-strict mode.'); - - // Test setting the user_id to 0 using setValue method. - $entity = entity_create('entity_test', array( - 'name' => 'test', - 'user_id' => $GLOBALS['user']->uid, - )); - $translation = $entity->getTranslation($this->langcodes[1], FALSE); - $translation->user_id->setValue(0); - $this->assertIdentical($entity->get('user_id')->value, 0); } /**