diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index acdc54e..9ab950b 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -130,11 +130,11 @@ public function loadMultiple(array $ids = NULL) { $queried_entities = $this->buildQuery($ids); } - // Pass all entities loaded from the database through $this->attachLoad(), + // Pass all entities loaded from the database through $this->postLoad(), // which calls the // entity type specific load callback, for example hook_node_type_load(). if (!empty($queried_entities)) { - $this->attachLoad($queried_entities); + $this->postLoad($queried_entities); $entities += $queried_entities; } diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php index 07377aa..dea470b 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php @@ -162,11 +162,11 @@ public function loadMultiple(array $ids = NULL) { $queried_entities = $query_result->fetchAllAssoc($this->idKey); } - // Pass all entities loaded from the database through $this->attachLoad(), + // Pass all entities loaded from the database through $this->postLoad(), // which attaches fields (if supported by the entity type) and calls the // entity type specific load callback, for example hook_node_load(). if (!empty($queried_entities)) { - $this->attachLoad($queried_entities); + $this->postLoad($queried_entities); $entities += $queried_entities; } @@ -214,11 +214,11 @@ public function loadRevision($revision_id) { } $queried_entities = $query_result->fetchAllAssoc($this->idKey); - // Pass the loaded entities from the database through $this->attachLoad(), + // Pass the loaded entities from the database through $this->postLoad(), // which attaches fields (if supported by the entity type) and calls the // entity type specific load callback, for example hook_node_load(). if (!empty($queried_entities)) { - $this->attachLoad($queried_entities, $revision_id); + $this->postLoad($queried_entities, $revision_id); } return reset($queried_entities); } @@ -336,25 +336,21 @@ protected function buildQuery($ids, $revision_id = FALSE) { /** * Attaches data to entities upon loading. * - * This will attach fields, if the entity is fieldable. It calls - * hook_entity_load() for modules which need to add data to all entities. - * It also calls hook_TYPE_load() on the loaded entities. For example - * hook_node_load() or hook_user_load(). If your hook_TYPE_load() - * expects special parameters apart from the queried entities, you can set - * $this->hookLoadArguments prior to calling the method. - * See Drupal\node\NodeStorageController::attachLoad() for an example. + * This will attach fields, if the entity is fieldable and calls the + * entity postLoad() method. * * @param $queried_entities * Associative array of query results, keyed on the entity ID. - * @param $load_revision - * (optional) TRUE if the revision should be loaded, defaults to FALSE. + * @param $revision_id + * ID of the revision that was loaded, or FALSE if the most current revision + * was loaded. */ - protected function attachLoad(&$queried_entities, $load_revision = FALSE) { + protected function postLoad(array &$queried_entities, $revision_id = FALSE) { // Attach field values. if ($this->entityInfo['fieldable']) { - $this->loadFieldItems($queried_entities, $load_revision ? static::FIELD_LOAD_REVISION : static::FIELD_LOAD_CURRENT); + $this->loadFieldItems($queried_entities, $revision_id ? static::FIELD_LOAD_REVISION : static::FIELD_LOAD_CURRENT); } - parent::attachLoad($queried_entities, $load_revision); + parent::postLoad($queried_entities, $revision_id); } /** diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 9fb88df..81b0790 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -214,10 +214,10 @@ protected function buildQuery($ids, $revision_id = FALSE) { * * Added mapping from storage records to entities. */ - protected function attachLoad(&$queried_entities, $load_revision = FALSE) { + protected function postLoad(&$queried_entities, $revision_id = FALSE) { // Map the loaded stdclass records into entity objects and according fields. - $queried_entities = $this->mapFromStorageRecords($queried_entities, $load_revision); - parent::attachLoad($queried_entities, $load_revision); + $queried_entities = $this->mapFromStorageRecords($queried_entities, $revision_id); + parent::postLoad($queried_entities, $revision_id); } /** @@ -225,13 +225,14 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) { * * @param array $records * Associative array of query results, keyed on the entity ID. - * @param boolean $load_revision - * (optional) TRUE if the revision should be loaded, defaults to FALSE. + * @param int $revision_id + * ID of the revision that was loaded, or FALSE if the most current revision + * was loaded. * * @return array * An array of entity objects implementing the EntityInterface. */ - protected function mapFromStorageRecords(array $records, $load_revision = FALSE) { + protected function mapFromStorageRecords(array $records, $revision_id = FALSE) { $entities = array(); foreach ($records as $id => $record) { $entities[$id] = array(); @@ -249,7 +250,7 @@ protected function mapFromStorageRecords(array $records, $load_revision = FALSE) $entities[$id] = new $this->entityClass($entities[$id], $this->entityType, $bundle); } } - $this->attachPropertyData($entities, $load_revision); + $this->attachPropertyData($entities, $revision_id); return $entities; } diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index cba3fff..6e04581 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -643,24 +643,15 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont } /** - * {@inheritdoc} + * Collects information for hook load. + * + * @param array $entities + * An array of entities. + * + * @return array + * A list of arguments to be passed by postLoad() to the entity load + * hooks. */ - public static function attachLoad(EntityStorageControllerInterface $storage_controller, $queried_entities, $load_revision) { - $entity_type = $storage_controller->entityType(); - // Call hook_entity_load(). - foreach (\Drupal::moduleHandler()->getImplementations('entity_load') as $module) { - $function = $module . '_entity_load'; - $function($queried_entities, $entity_type); - } - // 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), static::hookLoadArguments($queried_entities)); - foreach (\Drupal::moduleHandler()->getImplementations($entity_type . '_load') as $module) { - call_user_func_array($module . '_' . $entity_type . '_load', $args); - } - } - protected static function hookLoadArguments(array $entities) { return array(); } @@ -668,7 +659,20 @@ protected static function hookLoadArguments(array $entities) { /** * {@inheritdoc} */ - public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities) { + public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities, $revision_id = FALSE) { + $entity_type = $storage_controller->entityType(); + // Call hook_entity_load(). + foreach (\Drupal::moduleHandler()->getImplementations('entity_load') as $module) { + $function = $module . '_entity_load'; + $function($entities, $entity_type); + } + // 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), static::hookLoadArguments($entities)); + foreach (\Drupal::moduleHandler()->getImplementations($entity_type . '_load') as $module) { + call_user_func_array($module . '_' . $entity_type . '_load', $args); + } } /** diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index c0949f7..143b822 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -236,14 +236,17 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities); /** - * Acts on loaded entities before the load hook is invoked. + * Calls the entity load hooks. * * @param EntityStorageControllerInterface $storage_controller * The entity storage controller object. * @param array $entities * An array of entities. + * @param int|bool $revision_id + * ID of the revision that was loaded, or FALSE if the most current revision + * was loaded. */ - public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities); + public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities, $revision_id = FALSE); /** * Creates a duplicate of the entity. diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index 6a1675e..f7f5938 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -280,23 +280,15 @@ protected function invokeTranslationHooks(EntityInterface $entity) { /** * Attaches data to entities upon loading. * - * This will attach fields, if the entity is fieldable. It calls - * hook_entity_load() for modules which need to add data to all entities. - * It also calls hook_TYPE_load() on the loaded entities. For example - * hook_node_load() or hook_user_load(). If your hook_TYPE_load() - * expects special parameters apart from the queried entities, you can set - * $this->hookLoadArguments prior to calling the method. - * See Drupal\node\NodeStorageController::attachLoad() for an example. - * * @param $queried_entities * Associative array of query results, keyed on the entity ID. * @param $revision_id * ID of the revision that was loaded, or FALSE if the most current revision * was loaded. */ - protected function attachLoad(&$queried_entities, $revision_id = FALSE) { + protected function postLoad(&$queried_entities, $revision_id = FALSE) { $class = isset($this->entityInfo['class']) ? $this->entityInfo['class']: $this->entityClass; - $class::attachLoad($this, $queried_entities, $revision_id); + $class::postLoad($this, $queried_entities, $revision_id); } } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php index ad7506a..da396af 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php @@ -199,9 +199,9 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr /** * {@inheritdoc} */ - public static function attachLoad(EntityStorageControllerInterface $storage_controller, $queried_entities, $load_revision) { - $storage_controller->loadCategories($queried_entities); - parent::attachLoad($storage_controller, $queried_entities, $load_revision); + public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities) { + $storage_controller->loadCategories($entities); + parent::postLoad($storage_controller, $entities); } /** diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php index 15df66e..724b230 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php @@ -135,8 +135,8 @@ public function label($langcode = NULL) { /** * {@inheritdoc} */ - public static function attachLoad(EntityStorageControllerInterface $storage_controller, $queried_entities, $load_revision) { - $storage_controller->loadCategories($queried_entities); + public static function postLoad(EntityStorageControllerInterface $storage_controller, $entities, $revision_id = FALSE) { + $storage_controller->loadCategories($entities); parent::attachLoad($storage_controller, $queried_entities, $load_revision); } diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 6068ce9..84b8120 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -41,14 +41,14 @@ protected function buildQuery($ids, $revision_id = FALSE) { /** * {@inheritdoc} */ - protected function attachLoad(&$records, $load_revision = FALSE) { + protected function postLoad(&$records, $revision_id = FALSE) { // Prepare standard comment fields. foreach ($records as $key => $record) { $record->name = $record->uid ? $record->registered_name : $record->name; $record->node_type = 'comment_node_' . $record->node_type; $records[$key] = $record; } - parent::attachLoad($records, $load_revision); + parent::postLoad($records, $revision_id); } /** diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php index 7330147..f909751 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php @@ -105,12 +105,12 @@ protected function buildQuery($ids, $revision_id = FALSE) { } /** - * Overrides DatabaseStorageController::attachLoad(). + * Overrides DatabaseStorageController::postLoad(). * - * @todo Don't call parent::attachLoad() at all because we want to be able to + * @todo Don't call parent::postLoad() at all because we want to be able to * control the entity load hooks. */ - protected function attachLoad(&$menu_links, $load_revision = FALSE) { + protected function postLoad(&$menu_links, $load_revision = FALSE) { $routes = array(); foreach ($menu_links as &$menu_link) { @@ -141,7 +141,7 @@ protected function attachLoad(&$menu_links, $load_revision = FALSE) { } } - parent::attachLoad($menu_links, $load_revision); + parent::postLoad($menu_links, $load_revision); } /** diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Entity/ShortcutSet.php b/core/modules/shortcut/lib/Drupal/shortcut/Entity/ShortcutSet.php index b52fccb..b3d934d 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Entity/ShortcutSet.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Entity/ShortcutSet.php @@ -102,6 +102,19 @@ public function postCreate(EntityStorageControllerInterface $storage_controller) /** * {@inheritdoc} */ + public static function postLoad(EntityStorageControllerInterface $storage_controller, array $entities, $revision_id = FALSE) { + foreach ($entities as $id => $entity) { + $links = menu_load_links('shortcut-' . $id); + foreach ($links as $menu_link) { + $entity->links[$menu_link->uuid()] = $menu_link; + } + } + parent::postLoad($storage_controller, $entities, $revision_id); + } + + /** + * {@inheritdoc} + */ public function preSave(EntityStorageControllerInterface $storage_controller) { parent::preSave($storage_controller); diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php index 2f17850..7c0dee5 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php @@ -16,20 +16,6 @@ class ShortcutSetStorageController extends ConfigStorageController implements ShortcutSetStorageControllerInterface { /** - * Overrides \Drupal\config\ConfigStorageController::attachLoad(). - */ - protected function attachLoad(&$queried_entities, $revision_id = FALSE) { - parent::attachLoad($queried_entities, $revision_id); - - foreach ($queried_entities as $id => $entity) { - $links = menu_load_links('shortcut-' . $id); - foreach ($links as $menu_link) { - $entity->links[$menu_link->uuid()] = $menu_link; - } - } - } - - /** * {@inheritdoc} */ public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) { diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php index 67841cc..6769624 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php @@ -90,7 +90,6 @@ protected function init() { unset($this->type); } - /** * {@inheritdoc} */ diff --git a/core/modules/user/lib/Drupal/user/RoleStorageController.php b/core/modules/user/lib/Drupal/user/RoleStorageController.php index 163c232..e2c9ce9 100644 --- a/core/modules/user/lib/Drupal/user/RoleStorageController.php +++ b/core/modules/user/lib/Drupal/user/RoleStorageController.php @@ -27,11 +27,11 @@ public function deleteRoleReferences(array $rids) { /** * {@inheritdoc} */ - protected function attachLoad(&$queried_entities, $revision_id = FALSE) { + protected function postLoad(&$queried_entities, $revision_id = FALSE) { // Sort the queried roles by their weight. uasort($queried_entities, array($this->entityInfo['class'], 'sort')); - parent::attachLoad($queried_entities, $revision_id); + parent::postLoad($queried_entities, $revision_id); } } diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index 9cbcf0e..ee21a3f 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -75,9 +75,9 @@ public static function createInstance(ContainerInterface $container, $entity_typ } /** - * Overrides Drupal\Core\Entity\DatabaseStorageController::attachLoad(). + * Overrides Drupal\Core\Entity\DatabaseStorageController::postLoad(). */ - function attachLoad(&$queried_users, $load_revision = FALSE) { + function postLoad(&$queried_users, $load_revision = FALSE) { foreach ($queried_users as $key => $record) { $queried_users[$key]->roles = array(); if ($record->uid) { @@ -91,9 +91,9 @@ function attachLoad(&$queried_users, $load_revision = FALSE) { // Add any additional roles from the database. $this->addRoles($queried_users); - // Call the default attachLoad() method. This will add fields and call + // Call the default postLoad() method. This will add fields and call // hook_user_load(). - parent::attachLoad($queried_users, $load_revision); + parent::postLoad($queried_users, $load_revision); } /** diff --git a/core/modules/views/lib/Drupal/views/ViewStorageController.php b/core/modules/views/lib/Drupal/views/ViewStorageController.php index 087d34e..decc571 100644 --- a/core/modules/views/lib/Drupal/views/ViewStorageController.php +++ b/core/modules/views/lib/Drupal/views/ViewStorageController.php @@ -33,12 +33,12 @@ public function loadMultiple(array $ids = NULL) { /** * {@inheritdoc} */ - protected function attachLoad(&$queried_entities, $revision_id = FALSE) { + protected function postLoad(&$queried_entities, $revision_id = FALSE) { foreach ($queried_entities as $entity) { $entity->mergeDefaultDisplaysOptions(); } - parent::attachLoad($queried_entities, $revision_id); + parent::postLoad($queried_entities, $revision_id); }