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 { else {
$value = isset($this->definition['default_value']) ? $this->definition['default_value'] : NULL; $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. // Allow the field type to process default values.
$field_item_list_class = $this->getClass(); $field_item_list_class = $this->getClass();
return $field_item_list_class::processDefaultValue($value, $entity, $this); return $field_item_list_class::processDefaultValue($value, $entity, $this);
@ -405,8 +413,8 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI
* The entity being created. * The entity being created.
* - \Drupal\Core\Field\FieldDefinitionInterface $definition * - \Drupal\Core\Field\FieldDefinitionInterface $definition
* The field definition. * The field definition.
* It should return the default value as documented by * It should return the default value in the format accepted by the
* \Drupal\Core\Field\FieldDefinitionInterface::getDefaultValue(). * setDefaultValue() method.
* *
* @return $this * @return $this
*/ */
@ -425,8 +433,12 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI
* any value set here. * any value set here.
* *
* @param mixed $value * @param mixed $value
* The default value in the format as returned by * The default value for the field. This can be either:
* \Drupal\Core\Field\FieldDefinitionInterface::getDefaultValue(). * - 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 * @return $this
*/ */

View File

@ -340,6 +340,14 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn
else { else {
$value = $this->default_value; $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. // Allow the field type to process default values.
$field_item_list_class = $this->getClass(); $field_item_list_class = $this->getClass();
return $field_item_list_class::processDefaultValue($value, $entity, $this); 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 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity for which the default value is generated. * The entity for which the default value is generated.
* *
* @return mixed * @return array
* The default value for the field, as accepted by * The default value for the field, as a numerically indexed array of items,
* \Drupal\field\Plugin\Core\Entity\FieldItemListInterface::setValue(). This * each item being a property/value array (array() for no default value).
* 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.
*/ */
public function getDefaultValue(FieldableEntityInterface $entity); public function getDefaultValue(FieldableEntityInterface $entity);

View File

@ -242,25 +242,19 @@ interface FieldItemListInterface extends ListInterface, AccessibleInterface {
* Processes the default value before being applied. * Processes the default value before being applied.
* *
* Defined or configured default values of a field might need some processing * 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 * in order to be a valid runtime value for the field type; e.g., a date field
* process the defined value of 'NOW' to a valid date. * could process the defined value of 'NOW' to a valid date.
* *
* @param mixed * @param array
* The default value as defined for the field. * 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 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity for which the default value is generated. * The entity for which the default value is generated.
* @param \Drupal\Core\Field\FieldDefinitionInterface $definition * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
* The definition of the field. * The definition of the field.
* *
* @return mixed * @return array
* The default value for the field, as accepted by * The return default value for the field.
* \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.
*/ */
public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition); public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition);

View File

@ -161,15 +161,17 @@ class BaseFieldDefinitionTest extends UnitTestCase {
*/ */
public function testFieldDefaultValue() { public function testFieldDefaultValue() {
$definition = BaseFieldDefinition::create($this->fieldType); $definition = BaseFieldDefinition::create($this->fieldType);
$value = $this->randomMachineName(); $default_value = array(
$definition->setDefaultValue($value); 'value' => $this->randomMachineName(),
);
$definition->setDefaultValue($default_value);
$entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
// Set the field item list class to be used to avoid requiring the typed // Set the field item list class to be used to avoid requiring the typed
// data manager to retrieve it. // data manager to retrieve it.
$definition->setClass('Drupal\Core\Field\FieldItemList'); $definition->setClass('Drupal\Core\Field\FieldItemList');
$this->assertEquals($value, $definition->getDefaultValue($entity)); $this->assertEquals($default_value, $definition->getDefaultValue($entity));
} }
/** /**