diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 61481af..931a9a7 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Entity\Plugin\DataType\EntityReference; +use Drupal\Core\Field\PrepareCacheInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; use Drupal\Core\TypedData\DataDefinition; @@ -991,4 +992,38 @@ public function referencedEntities() { return $referenced_entities; } + /** + * {@inheritdoc} + */ + public function getCacheData() { + $data = $this->values; + $default_langcode = $this->getUntranslated()->language()->id; + foreach ($this->getTranslationLanguages() as $langcode => $language) { + $translation = $this->getTranslation($langcode); + // Make sure the default language is valid. + if ($default_langcode == $langcode) { + $langcode = Language::LANGCODE_DEFAULT; + } + foreach ($translation as $field_name => $items) { + if (!$items->isEmpty()) { + foreach ($items as $delta => $item) { + if (isset($data[$field_name][$langcode]) && !is_array($data[$field_name][$langcode])) { + $data[$field_name][$langcode] = array($data[$field_name][$langcode]); + } + // If the field item needs to be prepare the cache data, call + // the corresponding method, otherwise use the values as cache + // data. + if ($item instanceof PrepareCacheInterface) { + $data[$field_name][$langcode][$delta] = $item->getCacheData(); + } + else { + $data[$field_name][$langcode][$delta] = $item->getValue(); + } + } + } + } + } + return $data; + } + } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index 862b23f..93b1f63 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Field\PrepareCacheInterface; use Drupal\Core\TypedData\ComplexDataInterface; use Drupal\Core\TypedData\TranslatableInterface; @@ -26,7 +27,7 @@ * @see \Drupal\Core\TypedData\TypedDataManager * @see \Drupal\Core\Field\FieldItemListInterface */ -interface ContentEntityInterface extends EntityInterface, RevisionableInterface, TranslatableInterface, ComplexDataInterface { +interface ContentEntityInterface extends EntityInterface, RevisionableInterface, TranslatableInterface, ComplexDataInterface, PrepareCacheInterface { /** * Marks the translation identified by the given language code as existing. diff --git a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php index 3898882..eacbf53 100644 --- a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php @@ -198,10 +198,6 @@ public function loadMultiple(array $ids = NULL) { // Load remaining entities either from the persistent cache or storage. $entities = $this->doLoadMultiple($ids); - // Pass all entities loaded from the database through $this->postLoad(), - // to invoke the load hooks and postLoad() method on the entity class. - $this->postLoad($entities); - $this->setStaticCache($entities); $entities += $entities_from_static_cache; @@ -229,7 +225,6 @@ protected function doLoadMultiple(array $ids = NULL) { // Load any remaining entities from the database. $entities_from_storage = $this->getFromStorage($ids); - $this->setPersistentCache($entities_from_storage); return $entities_from_cache + $entities_from_storage; @@ -264,6 +259,10 @@ protected function getFromStorage(array $ids = NULL) { } } + // Pass all entities loaded from the database through $this->postLoad(), + // to invoke the load hooks and postLoad() method on the entity class. + $this->postLoad($entities); + return $entities; } @@ -360,31 +359,8 @@ protected function setPersistentCache($entities) { 'id' => $entity->id(), 'bundle' => $entity->bundle(), 'translations' => array_keys($entity->getTranslationLanguages()), - 'values' => array(), + 'values' => $entity->getCacheData(), ); - $default_langcode = $entity->getUntranslated()->language()->id; - foreach ($entity->getTranslationLanguages() as $langcode => $language) { - $translation = $entity->getTranslation($langcode); - // Make sure the default language is valid. - if ($default_langcode == $langcode) { - $langcode = Language::LANGCODE_DEFAULT; - } - foreach ($translation as $field_name => $items) { - if (!$items->isEmpty()) { - foreach ($items as $delta => $item) { - // If the field item needs to be prepare the cache data, call - // the corresponding method, otherwise use the values as cache - // data. - if ($item instanceof PrepareCacheInterface) { - $data['values'][$field_name][$langcode][$delta] = $item->getCacheData(); - } - else { - $data['values'][$field_name][$langcode][$delta] = $item->getValue(); - } - } - } - } - } $this->cacheBackend->set($this->buildCacheId($id), $data, CacheBackendInterface::CACHE_PERMANENT, array($this->entityTypeId . '_values' => TRUE)); } }