From 91acb77fa50949454d0fa0f76caa87f551bc8cca Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Fri, 3 Jan 2014 22:00:01 +0000 Subject: [PATCH] Issue #2027795 by Berdir, amateescu: Optimize content entity serialization. --- .../Drupal/Core/Entity/ContentEntityBase.php | 21 +++++++++++++++---- .../Field/ConfigEntityReferenceItemBase.php | 14 +++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index d62c4047b5..9d502d28b8 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 61e6a5ee5e..4d8fbf904b 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} */ -- GitLab