diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index f4b4e5c020f..1c2f4af8742 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -68,13 +68,16 @@ 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 (!is_array($values)) { + throw new InvalidArgumentException("An entity field requires a numerically indexed array of items as value."); + } + if (!empty($values)) { - - // 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."); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php index 3cb497be11e..278e151ba8e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFormTest.php @@ -41,7 +41,7 @@ class EntityFormTest extends WebTestBase { function testFormCRUD() { $langcode = LANGUAGE_NOT_SPECIFIED; $name1 = $this->randomName(8); - $name2 = $this->randomName(8); + $name2 = $this->randomName(10); $edit = array( 'name' => $name1, 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 e0b25fb759b..9c38fdaec19 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php @@ -171,6 +171,15 @@ 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); } /**