diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index d62c4047b57ae2f62783475d160362e5d9835ffb..9d502d28b8cb7b9a03f598d2326bf9d1d04aaddc 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -332,15 +332,28 @@ protected function clearTranslationCache() { } } + /** + * {@inheritdoc} + */ + 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->fields = array(); + $this->fieldDefinitions = NULL; + $this->clearTranslationCache(); + return array_keys(get_object_vars($this)); + } + + /** * Magic __wakeup() implementation. */ public function __wakeup() { $this->init(); - // @todo This should be done before serializing the entity, but we would - // need to provide the full list of data to be serialized. See the - // dedicated issue at https://drupal.org/node/2027795. - $this->clearTranslationCache(); } /** diff --git a/core/lib/Drupal/Core/Field/ConfigEntityReferenceItemBase.php b/core/lib/Drupal/Core/Field/ConfigEntityReferenceItemBase.php index 61e6a5ee5e2e7100d20c6760c31ac9d36a7958d5..4d8fbf904be1568cfb427a5b46d81b01bfe27f22 100644 --- a/core/lib/Drupal/Core/Field/ConfigEntityReferenceItemBase.php +++ b/core/lib/Drupal/Core/Field/ConfigEntityReferenceItemBase.php @@ -33,6 +33,20 @@ public function isEmpty() { return TRUE; } + /** + * {@inheritdoc} + */ + public function getValue() { + $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 (empty($this->target_id) && !empty($this->entity)) { + $values['entity'] = $this->entity; + } + return $values; + } + /** * {@inheritdoc} */