diff --git a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php index 92586bc..590ab9e 100644 --- a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php @@ -543,18 +543,7 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) { $this->loadFieldItems($queried_entities, $load_revision ? static::FIELD_LOAD_REVISION : static::FIELD_LOAD_CURRENT); } - // 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(). The first argument for hook_TYPE_load() are - // always the queried entities, followed by additional arguments set in - // $this->hookLoadArguments. - $args = array_merge(array($queried_entities), $this->hookLoadArguments); - foreach (\Drupal::moduleHandler()->getImplementations($this->entityType . '_load') as $module) { - call_user_func_array($module . '_' . $this->entityType . '_load', $args); - } + $this->invokeLoadHook($queried_entities); } /** @@ -1496,17 +1485,25 @@ protected function cacheGet($ids) { if ($cids && $cache = $this->cacheBackend->getMultiple($cids)) { // Put the cached field values back into the entities and remove them from // the list of entities to query. + $entities_from_cache = array(); foreach ($ids as $id) { $cid = "entity:{$this->entityType}:$id"; if (isset($cache[$cid])) { $values = $cache[$cid]->data['values']; $translations = $cache[$cid]->data['translations']; $bundle = $this->bundleKey ? $cache[$cid]->data['bundle'] : FALSE; - $entities[$id] = new $this->entityClass($values, $this->entityType, $bundle, $translations); + $entities_from_cache[$id] = new $this->entityClass($values, $this->entityType, $bundle, $translations); // Already put the loaded entity into the cache. - $this->entityCache[$id] = $entities[$id]; + $this->entityCache[$id] = $entities_from_cache[$id]; } } + + // Call the load hooks on the entities loaded from the persistent cache. + if (!empty($entities_from_cache)) { + $this->invokeLoadHook($entities_from_cache); + $entities = array_merge($entities, $entities_from_cache); + } + } return $entities; } @@ -1577,4 +1574,27 @@ public function resetCache(array $ids = NULL) { } } + /** + * Invokes the entity load hooks on the given entities. + * + * @param array $entities + * List of entities to invoke the hook for. + */ + protected function invokeLoadHook($entities) { + // Call hook_entity_load(). + foreach (\Drupal::moduleHandler() + ->getImplementations('entity_load') as $module) { + $function = $module . '_entity_load'; + $function($entities, $this->entityType); + } + // Call hook_TYPE_load(). The first argument for hook_TYPE_load() are + // always the queried entities, followed by additional arguments set in + // $this->hookLoadArguments. + $args = array_merge(array($entities), $this->hookLoadArguments); + foreach (\Drupal::moduleHandler() + ->getImplementations($this->entityType . '_load') as $module) { + call_user_func_array($module . '_' . $this->entityType . '_load', $args); + } + } + } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php index 2078f58..bf4c27c 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockStorageController.php @@ -19,21 +19,19 @@ class CustomBlockStorageController extends FieldableDatabaseStorageController { /** - * Overrides \Drupal\Core\Entity\DatabaseStorageController::attachLoad(). + * {@ineheritdoc} */ - protected function attachLoad(&$blocks, $load_revision = FALSE) { - // Create an array of block types for passing as a load argument. - // Note that blocks at this point are still \StdClass objects returned from - // the database. - foreach ($blocks as $id => $entity) { - $types[$entity->type] = $entity->type; + protected function invokeLoadHook($entities) { + // Besides the list of custom blocks, pass one additional argument to + // hook_custom_block_load(), containing a list of block types that were loaded. + $typed_nodes = array(); + foreach ($entities as $id => $node) { + $typed_nodes[$node->bundle()][$id] = $entities[$id]; } + $argument = array_keys($typed_nodes); + $this->hookLoadArguments = array($argument); - // Besides the list of blocks, pass one additional argument to - // hook_custom_block_load(), containing a list of block types that were - // loaded. - $this->hookLoadArguments = array($types); - parent::attachLoad($blocks, $load_revision); + parent::invokeLoadHook($entities); } } diff --git a/core/modules/node/lib/Drupal/node/NodeStorageController.php b/core/modules/node/lib/Drupal/node/NodeStorageController.php index 0c3a623..ff2a4d8 100644 --- a/core/modules/node/lib/Drupal/node/NodeStorageController.php +++ b/core/modules/node/lib/Drupal/node/NodeStorageController.php @@ -30,46 +30,22 @@ public function create(array $values) { } /** - * Overrides Drupal\Core\Entity\DatabaseStorageController::attachLoad(). + * {@ineheritdoc} */ - protected function attachLoad(&$queried_entities, $load_revision = FALSE) { - $queried_entities = $this->mapFromStorageRecords($queried_entities, $load_revision); - - // Create an array of nodes for each content type and pass this to the - // object type specific callback. To preserve backward-compatibility we - // pass on BC decorators to node-specific hooks, while we pass on the - // regular entity objects else. - $typed_nodes = array(); - foreach ($queried_entities as $id => $node) { - $typed_nodes[$node->bundle()][$id] = $queried_entities[$id]; - } - - if ($load_revision) { - $this->loadFieldItems($queried_entities, static::FIELD_LOAD_REVISION); - } - else { - $this->loadFieldItems($queried_entities, static::FIELD_LOAD_CURRENT); - } - + protected function invokeLoadHook($entities) { // Besides the list of nodes, pass one additional argument to // hook_node_load(), containing a list of node types that were loaded. + $typed_nodes = array(); + foreach ($entities as $id => $node) { + $typed_nodes[$node->bundle()][$id] = $entities[$id]; + } $argument = array_keys($typed_nodes); $this->hookLoadArguments = array($argument); - // 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(). The first argument for hook_TYPE_load() are - // always the queried entities, followed by additional arguments set in - // $this->hookLoadArguments. - $args = array_merge(array($queried_entities), $this->hookLoadArguments); - foreach (\Drupal::moduleHandler()->getImplementations($this->entityType . '_load') as $module) { - call_user_func_array($module . '_' . $this->entityType . '_load', $args); - } + parent::invokeLoadHook($entities); } + /** * Overrides Drupal\Core\Entity\DatabaseStorageController::postDelete(). */