diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 1517b16..e1ec846 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -156,6 +156,16 @@ protected $loadedRevisionId; /** + * Whether to serialize the complete entity structure. + * + * If set to TRUE when the entity is being serialized a deep serialization + * will be performed and the flag will be automatically set back to FALSE. + * + * @var bool + */ + protected $deepSerialization = FALSE; + + /** * {@inheritdoc} */ public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { @@ -442,7 +452,7 @@ public function __sleep() { // Get the values of instantiated field objects, only serialize the values. foreach ($this->fields as $name => $fields) { foreach ($fields as $langcode => $field) { - $this->values[$name][$langcode] = $field->getValue(); + $this->values[$name][$langcode] = $field->getSerializationValue($this->deepSerialization); } } $this->fields = array(); @@ -450,12 +460,26 @@ public function __sleep() { $this->languages = NULL; $this->clearTranslationCache(); + // As during the serialization process referenced entities might also have + // the "deepSerialization" flag set the only place of unsetting it remains + // in the sleep method right after the entity is prepared for the + // serialization and before executing it. + // @see \Drupal\Core\Field\EntityReferenceFieldItemList::getSerializationValue(). + $this->deepSerialization = FALSE; + return parent::__sleep(); } /** * {@inheritdoc} */ + public function setDeepSerialization($deep_serialization = TRUE) { + $this->deepSerialization = $deep_serialization; + } + + /** + * {@inheritdoc} + */ public function id() { return $this->getEntityKey('id'); } @@ -825,6 +849,7 @@ protected function initializeTranslation($langcode) { $translation->enforceIsNew = &$this->enforceIsNew; $translation->newRevision = &$this->newRevision; $translation->entityKeys = &$this->entityKeys; + $translation->deepSerialization = &$this->deepSerialization; $translation->translatableEntityKeys = &$this->translatableEntityKeys; $translation->translationInitialize = FALSE; $translation->typedData = NULL; @@ -1095,7 +1120,8 @@ public function __clone() { // Ensure that the following properties are actually cloned by // overwriting the original references with ones pointing to copies of - // them: enforceIsNew, newRevision, loadedRevisionId and fields. + // them: enforceIsNew, newRevision, loadedRevisionId, fields and + // deepSerialization. $enforce_is_new = $this->enforceIsNew; $this->enforceIsNew = &$enforce_is_new; @@ -1108,6 +1134,9 @@ public function __clone() { $fields = $this->fields; $this->fields = &$fields; + $deepSerialization = $this->deepSerialization; + $this->deepSerialization = &$deepSerialization; + foreach ($this->fields as $name => $values) { $this->fields[$name] = array(); // Untranslatable fields may have multiple references for the same field diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php index d21a2a8..e6ed455 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php @@ -236,4 +236,19 @@ public function isValidationRequired(); */ public function setValidationRequired($required); + /** + * 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. + * + * @param bool $deep_serialization + * TRUE if deep serialization should be performed when the entity is being + * serialized, FALSE otherwise. Defaults to TRUE. + * + * @return $this + */ + public function setDeepSerialization($deep_serialization = TRUE); + } diff --git a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php index d21ac96..b471d4d 100644 --- a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php @@ -13,6 +13,25 @@ class EntityReferenceFieldItemList extends FieldItemList implements EntityRefere /** * {@inheritdoc} */ + public function getSerializationValue($deep_serialization) { + if ($deep_serialization) { + $values = $this->getValue(TRUE); + foreach ($values as $delta => $item_values) { + if (isset($values[$delta]['entity']) && ($values[$delta]['entity'] instanceof FieldableEntityInterface)) { + $values[$delta]['entity']->setDeepSerialization(); + } + } + } + else { + $values = parent::getSerializationValue($deep_serialization); + } + + return $values; + } + + /** + * {@inheritdoc} + */ public function getConstraints() { $constraints = parent::getConstraints(); $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager(); diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index 97dd493..ea530d1 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -121,6 +121,13 @@ public function setValue($values, $notify = TRUE) { /** * {@inheritdoc} */ + public function getSerializationValue($deep_serialization) { + return $this->getValue(); + } + + /** + * {@inheritdoc} + */ public function __get($property_name) { // For empty fields, $entity->field->property is NULL. if ($item = $this->first()) { diff --git a/core/lib/Drupal/Core/Field/FieldItemListInterface.php b/core/lib/Drupal/Core/Field/FieldItemListInterface.php index 4e77202..dfeeede 100644 --- a/core/lib/Drupal/Core/Field/FieldItemListInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemListInterface.php @@ -271,4 +271,14 @@ public static function processDefaultValue($default_value, FieldableEntityInterf */ public function equals(FieldItemListInterface $list_to_compare); + /** + * Gets the data value prepared for serialization. + * + * @param bool $deep_serialization + * Whether a deep serialization is running. + * + * @return mixed + */ + public function getSerializationValue($deep_serialization); + } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 3babf42..8907869 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -190,7 +190,7 @@ public function setValue($values, $notify = TRUE) { // If the entity has been saved and we're trying to set both the // target_id and the entity values with a non-null target ID, then the // value for target_id should match the ID of the entity value. - if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id !== $values['target_id'])) { + if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id != $values['target_id'])) { throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.'); } } @@ -205,12 +205,12 @@ public function setValue($values, $notify = TRUE) { /** * {@inheritdoc} */ - public function getValue() { + public function getValue($include_computed = FALSE) { $values = parent::getValue(); // If there is an unsaved entity, return it as part of the field item values // to ensure idempotency of getValue() / setValue(). - if ($this->hasNewEntity()) { + if (($include_computed && !$this->isEmpty()) || $this->hasNewEntity()) { $values['entity'] = $this->entity; } return $values; diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php new file mode 100644 index 0000000..4feee94 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntitySerializationTest.php @@ -0,0 +1,123 @@ +installEntitySchema($this->entityTypeId); + $this->entityStorage = $this->entityManager->getStorage($this->entityTypeId); + + // Create the test entity reference field. + FieldStorageConfig::create([ + 'field_name' => $this->entityReferenceFieldName, + 'entity_type' => $this->entityTypeId, + 'type' => 'entity_reference', + 'settings' => [ + 'target_type' => $this->entityTypeId, + ], + ])->save(); + FieldConfig::create([ + 'entity_type' => $this->entityTypeId, + 'field_name' => $this->entityReferenceFieldName, + 'bundle' => $this->entityTypeId, + 'translatable' => TRUE, + ])->save(); + } + + /** + * Tests regular and deep serialization. + */ + public function testSerialization() { + $this->doTestSerialization(FALSE); + $this->doTestSerialization(TRUE); + } + + /** + * Tests regular or deep serialization based on the parameter. + * + * @param bool $deep_serialization + * Whether to test regular or deep serialization. + */ + protected function doTestSerialization($deep_serialization) { + $field_name = $this->entityReferenceFieldName; + $user = $this->createUser(); + $initial_entity_name = 'test test'; + + // Create the test entities. + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity_level_zero */ + $entity_level_zero = $this->entityStorage->create([ + 'name' => $initial_entity_name, + 'user_id' => $user->id(), + 'language' => 'en', + ]); + $entity_level_zero->save(); + $entity_level_one = $entity_level_zero->createDuplicate(); + $entity_level_one->save(); + $entity_level_two = $entity_level_zero->createDuplicate(); + $entity_level_two->save(); + + $entity_level_zero->$field_name->appendItem($entity_level_one); + $entity_level_zero->save(); + $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'; + + if ($deep_serialization) { + $entity_level_zero->setDeepSerialization(TRUE); + } + $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. + $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()); + } + +}