Commit f33b5103 authored by alexpott's avatar alexpott

Issue #2347711 by yched, undertext: Fixed...

Issue #2347711 by yched, undertext: Fixed FieldItemlListInterface::processDefaultValue($default_value) is expected to massage polymorphic data.
parent c496dad8
......@@ -387,6 +387,14 @@ public function getDefaultValue(FieldableEntityInterface $entity) {
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 @@ public function getDefaultValue(FieldableEntityInterface $entity) {
* 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 @@ public function setDefaultValueCallback($callback) {
* 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
*/
......
......@@ -340,6 +340,14 @@ public function getDefaultValue(FieldableEntityInterface $entity) {
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);
......
......@@ -175,15 +175,9 @@ public function isRequired();
* @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);
......
......@@ -242,25 +242,19 @@ public function defaultValuesFormSubmit(array $element, array &$form, FormStateI
* 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);
......
......@@ -161,15 +161,17 @@ public function testDefaultFieldSettings() {
*/
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));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment