diff -u b/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php --- b/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -158,6 +158,10 @@ /** * Whether to serialize the complete entity structure. * + * Depending on the field setting "serialize_embedded_entities" of entity + * reference fields a deeply serialized entity might contain the referenced + * entities. + * * @var bool */ protected $deepSerialization = FALSE; reverted: --- b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -74,23 +74,4 @@ */ public function updateLoadedRevisionId(); - /** - * Returns a deeply serialized entity. - * - * @return string - * The deeply serialized entity. - */ - public function deepSerialize(); - - /** - * Flags the entity for deep serialization. - * - * After calling this method and serializing the entity e.g. - * "serialize($entity);" a deep serialization will be performed and the flag - * will be automatically reset. - * - * @return $this - */ - public function setDeepSerialization(); - } diff -u b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php --- b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -341,14 +341,6 @@ '#element_validate' => array(array(get_class($this), 'fieldSettingsFormValidate')), ); - - $form['serialize_embedded_entities'] = array( - '#type' => 'checkbox', - '#title' => $this->t('Serialize entity references together with the parent entity when performing deep serialization of the parent entity.'), - '#description' => $this->t('If the field widget to be used for this field is an "entity reference inline widget" then the setting must be set.'), - '#default_value' => $field->getSetting('serialize_embedded_entities'), - ); - $form['handler'] = array( '#type' => 'details', '#title' => t('Reference type'), diff -u b/core/modules/field/field.install b/core/modules/field/field.install --- b/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -115,24 +115,25 @@ * fields. */ function field_update_8300() { - $config = \Drupal::configFactory(); + $config_factory = \Drupal::configFactory(); /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */ $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); // Iterate over all fields. - foreach ($config->listAll('field.field.') as $field_id) { - $field = $config->getEditable($field_id); - $class = $field_type_manager->getPluginClass($field->get('field_type')); + foreach ($config_factory->listAll('field.field.') as $field_id) { + $field_config = $config_factory->getEditable($field_id); + $field_type = $field_config->get('field_type'); + $class = $field_type_manager->getPluginClass($field_type); // Deal only with entity reference fields and descendants. - if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) { - $settings = $field->get('settings'); + if (($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) && (strpos($field_type, 'entity_reference') === 0)) { + $settings = $field_config->get('settings'); if (!isset($settings['serialize_embedded_entities'])) { $settings['serialize_embedded_entities'] = FALSE; } - $field->set('settings', $settings)->save(TRUE); + $field_config->set('settings', $settings)->save(TRUE); } } } 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 @@ -24,33 +24,33 @@ * * @var string */ - protected $entityTypeId; + protected $entityTypeId = 'entity_test_mul'; /** * The EntityTestMul entity type storage. * * @var \Drupal\Core\Entity\ContentEntityStorageInterface */ - protected $entityTestMulStorage; + protected $entityStorage; + + /** + * The entity reference field name. + * + * @var string + */ + protected $entityReferenceFieldName = 'field_test_entity_reference'; /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->entityTypeId = 'entity_test_mul'; $this->installEntitySchema($this->entityTypeId); - $this->entityTestMulStorage = $this->entityManager->getStorage($this->entityTypeId); - } + $this->entityStorage = $this->entityManager->getStorage($this->entityTypeId); - /** - * Tests regular and deep serialization. - */ - public function testSerialization() { - $field_name = 'field_test_entity_reference'; // Create the test entity reference field. FieldStorageConfig::create([ - 'field_name' => $field_name, + 'field_name' => $this->entityReferenceFieldName, 'entity_type' => $this->entityTypeId, 'type' => 'entity_reference', 'settings' => [ @@ -63,9 +63,14 @@ 'bundle' => $this->entityTypeId, 'translatable' => TRUE, ])->save(); + } - $this->doTestSerialization(FALSE, $field_name); - $this->doTestSerialization(TRUE, $field_name); + /** + * Tests regular and deep serialization. + */ + public function testSerialization() { + $this->doTestSerialization(FALSE); + $this->doTestSerialization(TRUE); } /** @@ -73,10 +78,9 @@ * * @param bool $deep_serialization * Whether to test regular or deep serialization. - * @param bool $field_name - * The name of the entity reference field. */ - protected function doTestSerialization($deep_serialization, $field_name) { + protected function doTestSerialization($deep_serialization) { + $field_name = $this->entityReferenceFieldName; // Check that the 'target_bundle' setting contains the custom bundle. $field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $field_name); $field_config->setSetting('serialize_embedded_entities', $deep_serialization) @@ -87,7 +91,7 @@ // Create the test entities. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity_level_zero */ - $entity_level_zero = $this->entityTestMulStorage->create([ + $entity_level_zero = $this->entityStorage->create([ 'name' => $initial_entity_name, 'user_id' => $user->id(), 'language' => 'en', @@ -103,14 +107,17 @@ $entity_level_one->$field_name->appendItem($entity_level_two); $entity_level_one->save(); + // Change the entity values in memory but do not persist them into the + // storage. $entity_level_zero->name = 'entity level zero'; $entity_level_zero->$field_name->entity->name = 'entity level one'; $entity_level_zero->$field_name->entity->$field_name->entity->name = 'entity level two'; - $this->entityTestMulStorage->resetCache(); - - $serialized_entity = $entity_level_zero->deepSerialize(); + $serialized_entity = $deep_serialization ? $entity_level_zero->deepSerialize() : serialize($entity_level_zero); $entity_level_zero = unserialize($serialized_entity); + + // If regular serialization is tested then the changes on the referenced + // entities should be lost, but kept if deep serialization is tested. $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()); only in patch2: unchanged: --- a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php @@ -236,4 +236,23 @@ public function isValidationRequired(); */ public function setValidationRequired($required); + /** + * Returns a deeply serialized entity. + * + * @return string + * The deeply serialized entity. + */ + public function deepSerialize(); + + /** + * Flags the entity for deep serialization. + * + * After calling this method and serializing the entity e.g. + * "serialize($entity);" a deep serialization will be performed and the flag + * will be automatically reset. + * + * @return $this + */ + public function setDeepSerialization(); + }