diff -u b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php --- b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php @@ -15,14 +15,20 @@ * {@inheritdoc} */ public function getSerializationValue($deep_serialization) { - $values = parent::getSerializationValue($deep_serialization); + // When performing a deep serialization then we overwrite the parent method + // in order to include the computed properties i.e. in the current case the + // referenced entity objects and flag them for deep serialization as well. if ($deep_serialization) { + $values = $this->getValue(TRUE); foreach ($values as $delta => $item_values) { if (isset($values[$delta]['entity']) && ($values[$delta]['entity'] instanceof ContentEntityInterface)) { $values[$delta]['entity']->setDeepSerialization(TRUE); } } } + else { + $values = parent::getSerializationValue(FALSE); + } return $values; } diff -u b/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php --- b/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -122,7 +122,8 @@ * {@inheritdoc} */ public function getSerializationValue($deep_serialization) { - return $this->getValue($deep_serialization); + // By default do not return computed properties for deep serialization. + return $this->getValue(FALSE); } /** diff -u b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php --- b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php @@ -111,17 +111,18 @@ $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. + // that not all computed properties are being serialized through deep + // serialization but only computed properties of entity reference fields. $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'); + $changed_language_name = $original_language_name . ' changed'; + $test_language->setName($changed_language_name); $langcode_field->language = $test_language; - $this->assertEquals('test deep serialization on language property', $langcode_field->language->getName()); + $this->assertEquals($changed_language_name, $langcode_field->language->getName()); if ($deep_serialization) { $entity_level_zero->setDeepSerialization(TRUE); @@ -132,13 +133,15 @@ // method - regular or deep serialization. $entity_level_zero = unserialize(serialize($entity_level_zero)); - // If regular serialization is tested then the changes on the referenced - // entities should be lost, but kept if deep serialization is tested. + // If regular serialization is tested then the changes on all computed + // property values should be lost. If deep serialization is tested then the + // changes only on computed properties of entity reference fields should be + // kept, but changes on computed properties of other fields should be lost. $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()); + $this->assertEquals($original_language_name, $language->getName()); } /**