From f33b5103651dfbef32a5dde10d8da19e589b4dda Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 7 Oct 2014 16:49:44 +0100 Subject: [PATCH] Issue #2347711 by yched, undertext: Fixed FieldItemlListInterface::processDefaultValue($default_value) is expected to massage polymorphic data. --- .../Drupal/Core/Field/BaseFieldDefinition.php | 20 +++++++++++++++---- .../lib/Drupal/Core/Field/FieldConfigBase.php | 8 ++++++++ .../Core/Field/FieldDefinitionInterface.php | 12 +++-------- .../Core/Field/FieldItemListInterface.php | 20 +++++++------------ .../Core/Entity/BaseFieldDefinitionTest.php | 8 +++++--- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index 1dddd95a7eb7..9b04e9c8c018 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -387,6 +387,14 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI else { $value = isset($this->definition['default_value']) ? $this->definition['default_value'] : NULL; } + // Normalize into the "array keyed by delta" format. + if (isset($value) && !is_array($value)) { + $properties = $this->getPropertyNames(); + $property = reset($properties); + $value = array( + array($property => $value), + ); + } // Allow the field type to process default values. $field_item_list_class = $this->getClass(); return $field_item_list_class::processDefaultValue($value, $entity, $this); @@ -405,8 +413,8 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI * The entity being created. * - \Drupal\Core\Field\FieldDefinitionInterface $definition * The field definition. - * It should return the default value as documented by - * \Drupal\Core\Field\FieldDefinitionInterface::getDefaultValue(). + * It should return the default value in the format accepted by the + * setDefaultValue() method. * * @return $this */ @@ -425,8 +433,12 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI * any value set here. * * @param mixed $value - * The default value in the format as returned by - * \Drupal\Core\Field\FieldDefinitionInterface::getDefaultValue(). + * The default value for the field. This can be either: + * - a literal, in which case it will be assigned to the first property of + * the first item. + * - a numerically indexed array of items, each item being a property/value + * array. + * - NULL or array() for no default value. * * @return $this */ diff --git a/core/lib/Drupal/Core/Field/FieldConfigBase.php b/core/lib/Drupal/Core/Field/FieldConfigBase.php index 32e695f946d1..05fc94bf7912 100644 --- a/core/lib/Drupal/Core/Field/FieldConfigBase.php +++ b/core/lib/Drupal/Core/Field/FieldConfigBase.php @@ -340,6 +340,14 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn else { $value = $this->default_value; } + // Normalize into the "array keyed by delta" format. + if (isset($value) && !is_array($value)) { + $properties = $this->getFieldStorageDefinition()->getPropertyNames(); + $property = reset($properties); + $value = array( + array($property => $value), + ); + } // Allow the field type to process default values. $field_item_list_class = $this->getClass(); return $field_item_list_class::processDefaultValue($value, $entity, $this); diff --git a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php index 06ce9d598ad2..8376d299bf31 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php @@ -175,15 +175,9 @@ interface FieldDefinitionInterface extends ListDataDefinitionInterface { * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity for which the default value is generated. * - * @return mixed - * The default value for the field, as accepted by - * \Drupal\field\Plugin\Core\Entity\FieldItemListInterface::setValue(). This - * can be either: - * - a literal, in which case it will be assigned to the first property of - * the first item. - * - a numerically indexed array of items, each item being a property/value - * array. - * - NULL or array() for no default value. + * @return array + * The default value for the field, as a numerically indexed array of items, + * each item being a property/value array (array() for no default value). */ public function getDefaultValue(FieldableEntityInterface $entity); diff --git a/core/lib/Drupal/Core/Field/FieldItemListInterface.php b/core/lib/Drupal/Core/Field/FieldItemListInterface.php index 7ec91c9b606e..fcaa9b1b8784 100644 --- a/core/lib/Drupal/Core/Field/FieldItemListInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemListInterface.php @@ -242,25 +242,19 @@ interface FieldItemListInterface extends ListInterface, AccessibleInterface { * Processes the default value before being applied. * * Defined or configured default values of a field might need some processing - * in order to be a valid value for the field type; e.g., a date field could - * process the defined value of 'NOW' to a valid date. + * in order to be a valid runtime value for the field type; e.g., a date field + * could process the defined value of 'NOW' to a valid date. * - * @param mixed - * The default value as defined for the field. + * @param array + * The unprocessed default value defined for the field, as a numerically + * indexed array of items, each item being an array of property/value pairs. * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity for which the default value is generated. * @param \Drupal\Core\Field\FieldDefinitionInterface $definition * The definition of the field. * - * @return mixed - * The default value for the field, as accepted by - * \Drupal\field\Plugin\Core\Entity\FieldItemListInterface::setValue(). This - * can be either: - * - a literal, in which case it will be assigned to the first property of - * the first item. - * - a numerically indexed array of items, each item being a property/value - * array. - * - NULL or array() for no default value. + * @return array + * The return default value for the field. */ public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition); diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index 17cb5182bc98..bc88b218140c 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -161,15 +161,17 @@ class BaseFieldDefinitionTest extends UnitTestCase { */ public function testFieldDefaultValue() { $definition = BaseFieldDefinition::create($this->fieldType); - $value = $this->randomMachineName(); - $definition->setDefaultValue($value); + $default_value = array( + 'value' => $this->randomMachineName(), + ); + $definition->setDefaultValue($default_value); $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') ->disableOriginalConstructor() ->getMock(); // Set the field item list class to be used to avoid requiring the typed // data manager to retrieve it. $definition->setClass('Drupal\Core\Field\FieldItemList'); - $this->assertEquals($value, $definition->getDefaultValue($entity)); + $this->assertEquals($default_value, $definition->getDefaultValue($entity)); } /**