diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index 13959d5..70f52b7 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -164,17 +164,19 @@ protected function invokeHook($hook, EntityInterface $entity) { * Associative array of query results, keyed on the entity ID. */ protected function postLoad(array &$queried_entities) { - $entity_class = $this->entityInfo['class']; - $entity_class::postLoad($this, $queried_entities); - // Call hook_entity_load(). - foreach (\Drupal::moduleHandler()->getImplementations('entity_load') as $module) { - $function = $module . '_entity_load'; - $function($queried_entities, $this->entityType); - } - // Call hook_TYPE_load(). - foreach (\Drupal::moduleHandler()->getImplementations($this->entityType . '_load') as $module) { - $function = $module . '_' . $this->entityType . '_load'; - $function($queried_entities); + if (!empty($queried_entities)) { + $entity_class = $this->entityInfo['class']; + $entity_class::postLoad($this, $queried_entities); + // Call hook_entity_load(). + foreach (\Drupal::moduleHandler()->getImplementations('entity_load') as $module) { + $function = $module . '_entity_load'; + $function($queried_entities, $this->entityType); + } + // Call hook_TYPE_load(). + foreach (\Drupal::moduleHandler()->getImplementations($this->entityType . '_load') as $module) { + $function = $module . '_' . $this->entityType . '_load'; + $function($queried_entities); + } } } diff --git a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php index 1f94550..727a46b 100644 --- a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php @@ -233,8 +233,6 @@ public function create(array $values) { * {@inheritdoc} */ public function loadMultiple(array $ids = NULL) { - $entities = array(); - // Create a new variable which is either a prepared version of the $ids // array for later comparison with the entity cache, or FALSE if no $ids // were passed. The $ids array is reduced as items are loaded from cache, @@ -244,31 +242,54 @@ public function loadMultiple(array $ids = NULL) { // Try to load entities from the static cache, if the entity type supports // static caching. This will remove ID's that were loaded from $ids. - if ($this->staticCache && $ids) { - $entities += $this->getFromStaticCache($ids); - } + $entities_from_static_cache = $this->getFromStaticCache($ids); - // Load remaining entities from the persistent cache. - if ($this->persistentCache && $ids) { - $loaded_enities = $this->getFromPersistentCache($ids); - // Pass all entities loaded from the persistent cache through - // $this->postLoad() to invoke the load hooks and postLoad() method on - // the entity class. - // @todo: Avoid this by caching non-field values of the entity. - if (!empty($loaded_enities)) { - $this->postLoad($loaded_enities); - // Also put entities from the persistent cache in the static cache. - if ($this->staticCache) { - $this->setStaticCache($loaded_enities); - } + $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 += $loaded_enities; + $entities += $entities_from_static_cache; + + // Ensure that the returned array is ordered the same as the original + // $ids array if this was passed in and remove any invalid ids. + if ($passed_ids) { + // Remove any invalid ids from the array. + $passed_ids = array_intersect_key($passed_ids, $entities); + foreach ($entities as $entity) { + $passed_ids[$entity->id()] = $entity; } + $entities = $passed_ids; + } + + return $entities; + } + + protected function doLoadMultiple($ids) { + if (is_array($ids) && empty($ids)) { + return array(); } + // Load remaining entities from the persistent cache. + $entities_from_cache = $this->loadMultipleFromCache($ids); + // Load any remaining entities from the database. This is the case if $ids // is set to NULL (so we load all entities) or if there are any ids left to // load. + $entities_from_storage = $this->loadMultipleFromStorage($ids); + + $this->setPersistentCache($entities_from_storage); + + return $entities_from_cache + $entities_from_storage; + } + + protected function loadMultipleFromStorage($ids) { + + $entities = array(); + if ($ids === NULL || $ids) { // Build and execute the query. $query_result = $this->buildQuery($ids)->execute(); @@ -276,46 +297,27 @@ public function loadMultiple(array $ids = NULL) { // Map the loaded records into entity objects and according fields. if ($records) { - $queried_entities = $this->mapFromStorageRecords($records); + $entities = $this->mapFromStorageRecords($records); // Attach field values. if ($this->entityInfo['fieldable']) { - $this->loadFieldItems($queried_entities); + $this->loadFieldItems($entities); } - - // Pass all entities loaded from the database through $this->postLoad(), - // to invoke the load hooks and postLoad() method on the entity class. - $this->postLoad($queried_entities); - - // Add entities to the static cache. - if ($this->staticCache) { - $this->cacheSet($queried_entities); - } - - // Add them to the caches. - if ($this->persistentCache) { - $this->setPersistentCache($queried_entities); - } - if ($this->staticCache) { - $this->setStaticCache($queried_entities); - } - $entities += $queried_entities; } - } - // Ensure that the returned array is ordered the same as the original - // $ids array if this was passed in and remove any invalid ids. - if ($passed_ids) { - // Remove any invalid ids from the array. - $passed_ids = array_intersect_key($passed_ids, $entities); - foreach ($entities as $entity) { - $passed_ids[$entity->id()] = $entity; - } - $entities = $passed_ids; + return $entities; + } + + protected function loadMultipleFromCache($ids) { + if (!$this->persistentCache) { + return array(); } - return $entities; + $ids = !empty($ids) ? $ids : array(); + + // Load remaining entities from the persistent cache. + return $this->getFromPersistentCache($ids); } /** @@ -1585,13 +1587,17 @@ static public function _fieldColumnName(FieldInterface $field, $column) { */ protected function getFromStaticCache(&$ids) { $entities = array(); - // Load any available entities from the internal cache. - if (!empty($this->entities)) { - foreach ($ids as $index => $id) { - if (isset($this->entities[$id])) { - $entities[$id] = $this->entities[$id]; - // Remove the ID from the list. - unset($ids[$index]); + + if ($this->staticCache && $ids) { + $entities = array(); + // Load any available entities from the internal cache. + if (!empty($this->entities)) { + foreach ($ids as $index => $id) { + if (isset($this->entities[$id])) { + $entities[$id] = $this->entities[$id]; + // Remove the ID from the list. + unset($ids[$index]); + } } } } @@ -1605,7 +1611,9 @@ protected function getFromStaticCache(&$ids) { * Entities to store in the cache. */ protected function setStaticCache(array $entities) { - $this->entities += $entities; + if ($this->staticCache) { + $this->entities += $entities; + } } /** @@ -1638,6 +1646,10 @@ protected function getFromPersistentCache(&$ids) { * {@inheritdoc} */ protected function setPersistentCache($entities) { + if (!$this->persistentCache) { + return; + } + foreach ($entities as $id => $entity) { $data = array( 'id' => $entity->id(), diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php old mode 100644 new mode 100755