Issue #2347711 by yched, undertext: Fixed FieldItemlListInterface::processDefaultValue($default_value) is expected to massage polymorphic data.

8.0.x
Alex Pott 2014-10-07 16:49:44 +01:00
parent c496dad8db
commit f33b510365
5 changed files with 39 additions and 29 deletions

View File

@ -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
*/

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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));
}
/**