diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index bbcd55f..02ba043 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -122,7 +122,7 @@ public function setValue($values, $notify = TRUE) { * {@inheritdoc} */ public function getSerializationValue($deep_serialization) { - return $this->getValue(); + return $this->getValue($deep_serialization); } /** diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php index 18935c1..52a3f34 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php @@ -52,6 +52,27 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel } /** + * {@inheritdoc} + */ + public function getValue($include_computed = FALSE) { + $value = parent::getValue(); + + // Include computed properties if the condition of + // ::includeComputedProperties() is fulfilled. + if ($this->includeComputedProperties($include_computed)) { + // Iterate only over computed properties and include them, as the parent + // method is not including them. + foreach ($this->properties as $name => $property) { + $definition = $property->getDataDefinition(); + if ($definition->isComputed()) { + $value[$name] = $property->getValue(); + } + } + } + return $value; + } + + /** * Defines allowed language codes for the field's AllowedValues constraint. * * @return string[] diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php index c109886..f40d4ed 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php @@ -4,6 +4,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\language\Entity\ConfigurableLanguage; /** * Tests the serialization of content entities. @@ -17,7 +18,7 @@ class ContentEntitySerializationTest extends EntityKernelTestBase { * * @var array */ - public static $modules = ['user', 'system', 'field', 'entity_test']; + public static $modules = ['user', 'system', 'field', 'entity_test', 'language']; /** * The entity type id. @@ -46,6 +47,7 @@ class ContentEntitySerializationTest extends EntityKernelTestBase { protected function setUp() { parent::setUp(); $this->installEntitySchema($this->entityTypeId); + $this->installConfig('language'); $this->entityStorage = $this->entityManager->getStorage($this->entityTypeId); // Create the test entity reference field. @@ -108,6 +110,19 @@ protected function doTestSerialization($deep_serialization) { $entity_level_zero->$field_name->entity->name = 'entity level one'; $entity_level_zero->$field_name->entity->$field_name->entity->name = 'entity level two'; + // Alter a computed property beside the entity property in order to assure + // that all computed properties are being serialized through deep + // serialization. + $entity_type = $entity_level_two->getEntityType(); + $this->assertTrue($entity_type->hasKey('langcode')); + $langcode_field_name = $entity_type->getKey('langcode'); + $langcode_field = $entity_level_zero->$field_name->entity->$field_name->entity->$langcode_field_name; + $test_language = ConfigurableLanguage::load($langcode_field->language->getId()); + $original_language_name = $test_language->getName(); + $test_language->setName('test deep serialization on language property'); + $langcode_field->language = $test_language; + $this->assertEquals('test deep serialization on language property', $langcode_field->language->getName()); + if ($deep_serialization) { $entity_level_zero->setDeepSerialization(TRUE); } @@ -122,6 +137,8 @@ protected function doTestSerialization($deep_serialization) { $this->assertEquals('entity level zero', $entity_level_zero->label()); $this->assertEquals($deep_serialization ? 'entity level one' : $initial_entity_name, $entity_level_zero->$field_name->entity->label()); $this->assertEquals($deep_serialization ? 'entity level two' : $initial_entity_name, $entity_level_zero->$field_name->entity->$field_name->entity->label()); + $language = $entity_level_zero->$field_name->entity->$field_name->entity->$langcode_field_name->language; + $this->assertEquals($deep_serialization ? 'test deep serialization on language property' : $original_language_name, $language->getName()); } }