Commit 1290d389 authored by alexpott's avatar alexpott
Browse files

Issue #2382493 by yched: Population of default field values in entity translation is incorrect

parent 07714aca
......@@ -650,7 +650,7 @@ public function addTranslation($langcode, array $values = array()) {
foreach ($entity as $name => $field) {
if (!isset($values[$name]) && !$field->isEmpty()) {
$values[$name] = $field->value;
$values[$name] = $field->getValue();
}
}
......
......@@ -61,7 +61,18 @@ public function testDefaultValueCallback() {
$entity = $this->entityManager->getStorage('entity_test_default_value')->create();
// The description field has a default value callback for testing, see
// entity_test_field_default_value().
$this->assertEqual($entity->description->value, 'description_' . $entity->language()->getId());
$string = 'description_' . $entity->language()->getId();
$expected = array(
array(
'shape' => "shape:0:$string",
'color' => "color:0:$string",
),
array(
'shape' => "shape:1:$string",
'color' => "color:1:$string",
),
);
$this->assertEqual($entity->description->getValue(), $expected);
}
}
......@@ -434,16 +434,26 @@ function testEntityTranslationAPI() {
$translation->type->value = strrev($type);
$this->assertEqual($cloned->type->value, $translation->type->value, 'Untranslatable field references keep working after serializing and cloning the entity.');
// Check that per-language defaults are properly populated.
$entity = $this->reloadEntity($entity);
$field_id = implode('.', array($entity->getEntityTypeId(), $entity->bundle(), $this->field_name));
$field = $this->entityManager->getStorage('field_config')->load($field_id);
$field->default_value_callback = 'entity_test_field_default_value';
$field->save();
// Check that per-language defaults are properly populated. The
// 'entity_test_mul_default_value' entity type is translatable and uses
// entity_test_field_default_value() as a "default value callback" for its
// 'description' field.
$entity = $this->entityManager
->getStorage('entity_test_mul_default_value')
->create(array('name' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
$translation = $entity->addTranslation($langcode2);
$field_storage = $translation->get($this->field_name);
$this->assertEqual($field_storage->value, $this->field_name . '_' . $langcode2, 'Language-aware default values correctly populated.');
$this->assertEqual($field_storage->getLangcode(), $langcode2, 'Field object has the expected langcode.');
$expected = array(
array(
'shape' => "shape:0:description_$langcode2",
'color' => "color:0:description_$langcode2",
),
array(
'shape' => "shape:1:description_$langcode2",
'color' => "color:1:description_$langcode2",
),
);
$this->assertEqual($translation->description->getValue(), $expected, 'Language-aware default values correctly populated.');
$this->assertEqual($translation->description->getLangcode(), $langcode2, 'Field object has the expected langcode.');
}
/**
......
......@@ -443,7 +443,20 @@ function entity_test_entity_test_mul_translation_delete(EntityInterface $transla
* @see \Drupal\field\Entity\FieldConfig::$default_value
*/
function entity_test_field_default_value(FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
return array(array('value' => $definition->getName() . '_' . $entity->language()->getId()));
// Include the field name and entity language in the generated values to check
// that they are correctly passed.
$string = $definition->getName() . '_' . $entity->language()->getId();
// Return a "default value" with multiple items.
return array(
array(
'shape' => "shape:0:$string",
'color' => "color:0:$string",
),
array(
'shape' => "shape:1:$string",
'color' => "color:1:$string",
),
);
}
/**
......
......@@ -32,7 +32,7 @@ class EntityTestDefaultValue extends EntityTest {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['description'] = BaseFieldDefinition::create('string')
$fields['description'] = BaseFieldDefinition::create('shape')
->setLabel(t('Some custom description'))
->setDefaultValueCallback('entity_test_field_default_value');
......
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTestMulDefaultValue.
*/
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Defines the test entity class.
*
* @ContentEntityType(
* id = "entity_test_mul_default_value",
* label = @Translation("Test entity - data table"),
* handlers = {
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
* "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
* "form" = {
* "default" = "Drupal\entity_test\EntityTestForm",
* "delete" = "Drupal\entity_test\EntityTestDeleteForm"
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler",
* "views_data" = "Drupal\views\EntityViewsData"
* },
* base_table = "entity_test_mul",
* data_table = "entity_test_mul_property_data",
* translatable = TRUE,
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name"
* },
* links = {
* "canonical" = "entity.entity_test_mul.edit_form",
* "edit-form" = "entity.entity_test_mul.edit_form",
* "delete-form" = "entity.entity_test_mul.delete_form",
* },
* field_ui_base_route = "entity.entity_test_mul.admin_form",
* )
*/
class EntityTestMulDefaultValue extends EntityTestMul {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['description'] = BaseFieldDefinition::create('shape')
->setLabel(t('Some custom description'))
->setTranslatable(TRUE)
->setDefaultValueCallback('entity_test_field_default_value');
return $fields;
}
}
......@@ -2,24 +2,22 @@
/**
* @file
* Contains \Drupal\field_test\Plugin\Field\FieldType\ShapeItem.
* Contains \Drupal\entity_test\Plugin\Field\FieldType\ShapeItem.
*/
namespace Drupal\field_test\Plugin\Field\FieldType;
namespace Drupal\entity_test\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
/**
* Defines the 'shape_field' entity field item.
* Defines the 'shape' field type.
*
* @FieldType(
* id = "shape",
* label = @Translation("Shape"),
* description = @Translation("Another dummy field type."),
* default_widget = "test_field_widget",
* default_formatter = "field_test_default"
* )
*/
class ShapeItem extends FieldItemBase {
......
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