Skip to content
Snippets Groups Projects
Commit 1290d389 authored by Alex Pott's avatar Alex Pott
Browse files

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

parent 07714aca
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -650,7 +650,7 @@ public function addTranslation($langcode, array $values = array()) { ...@@ -650,7 +650,7 @@ public function addTranslation($langcode, array $values = array()) {
foreach ($entity as $name => $field) { foreach ($entity as $name => $field) {
if (!isset($values[$name]) && !$field->isEmpty()) { if (!isset($values[$name]) && !$field->isEmpty()) {
$values[$name] = $field->value; $values[$name] = $field->getValue();
} }
} }
......
...@@ -61,7 +61,18 @@ public function testDefaultValueCallback() { ...@@ -61,7 +61,18 @@ public function testDefaultValueCallback() {
$entity = $this->entityManager->getStorage('entity_test_default_value')->create(); $entity = $this->entityManager->getStorage('entity_test_default_value')->create();
// The description field has a default value callback for testing, see // The description field has a default value callback for testing, see
// entity_test_field_default_value(). // 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() { ...@@ -434,16 +434,26 @@ function testEntityTranslationAPI() {
$translation->type->value = strrev($type); $translation->type->value = strrev($type);
$this->assertEqual($cloned->type->value, $translation->type->value, 'Untranslatable field references keep working after serializing and cloning the entity.'); $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. // Check that per-language defaults are properly populated. The
$entity = $this->reloadEntity($entity); // 'entity_test_mul_default_value' entity type is translatable and uses
$field_id = implode('.', array($entity->getEntityTypeId(), $entity->bundle(), $this->field_name)); // entity_test_field_default_value() as a "default value callback" for its
$field = $this->entityManager->getStorage('field_config')->load($field_id); // 'description' field.
$field->default_value_callback = 'entity_test_field_default_value'; $entity = $this->entityManager
$field->save(); ->getStorage('entity_test_mul_default_value')
->create(array('name' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
$translation = $entity->addTranslation($langcode2); $translation = $entity->addTranslation($langcode2);
$field_storage = $translation->get($this->field_name); $expected = array(
$this->assertEqual($field_storage->value, $this->field_name . '_' . $langcode2, 'Language-aware default values correctly populated.'); array(
$this->assertEqual($field_storage->getLangcode(), $langcode2, 'Field object has the expected langcode.'); '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 ...@@ -443,7 +443,20 @@ function entity_test_entity_test_mul_translation_delete(EntityInterface $transla
* @see \Drupal\field\Entity\FieldConfig::$default_value * @see \Drupal\field\Entity\FieldConfig::$default_value
*/ */
function entity_test_field_default_value(FieldableEntityInterface $entity, FieldDefinitionInterface $definition) { 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 { ...@@ -32,7 +32,7 @@ class EntityTestDefaultValue extends EntityTest {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type); $fields = parent::baseFieldDefinitions($entity_type);
$fields['description'] = BaseFieldDefinition::create('string') $fields['description'] = BaseFieldDefinition::create('shape')
->setLabel(t('Some custom description')) ->setLabel(t('Some custom description'))
->setDefaultValueCallback('entity_test_field_default_value'); ->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 @@ ...@@ -2,24 +2,22 @@
/** /**
* @file * @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\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldItemBase;
/** /**
* Defines the 'shape_field' entity field item. * Defines the 'shape' field type.
* *
* @FieldType( * @FieldType(
* id = "shape", * id = "shape",
* label = @Translation("Shape"), * label = @Translation("Shape"),
* description = @Translation("Another dummy field type."), * description = @Translation("Another dummy field type."),
* default_widget = "test_field_widget",
* default_formatter = "field_test_default"
* ) * )
*/ */
class ShapeItem extends FieldItemBase { class ShapeItem extends FieldItemBase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment