diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index b127a229..a96bdd4 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -438,16 +438,21 @@ public function setDefaultValueCallback($callback) { * the first item. * - a numerically indexed array of items, each item being a property/value * array. + * - a non-numerically indexed array, in which case the array is assumed to + * be a property/value array and used as the first item * - NULL or array() for no default value. * * @return $this */ public function setDefaultValue($value) { - if (!is_array($value)) { - $value = array(array($this->getMainPropertyName() => $value)); - } - elseif (!is_numeric(array_keys($value)[0])) { - $value = array(0 => $value); + // Unless the value is NULL or an empty array, we may need to transform it. + if (!(is_null($value) || (is_array($value) && empty($value)))) { + if (!is_array($value)) { + $value = array(array($this->getMainPropertyName() => $value)); + } + elseif (!is_numeric(array_keys($value)[0])) { + $value = array(0 => $value); + } } $this->definition['default_value'] = $value; return $this; diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index ae71170..2b1b3ce 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -189,6 +189,14 @@ public function testFieldDefaultValue() { // Set default value with an indexed array. $definition->setDefaultValue($expected_default_value); $this->assertEquals($expected_default_value, $definition->getDefaultValue($entity)); + + // Set default value with an empty array. + $definition->setDefaultValue(array()); + $this->assertEquals(array(), $definition->getDefaultValue($entity)); + + // Set default value with NULL. + $definition->setDefaultValue(NULL); + $this->assertEquals(NULL, $definition->getDefaultValue($entity)); } /**