Commit 26f1c5f5 authored by Derek Wright's avatar Derek Wright
Browse files

[#2570593]: Add special case to EntityStorageBase::postLoad() for which entities to pass.

- If there's only a single class involved, pass the original $entities array.
- If there are multiple bundle classes, pass each one the matching entities from $items.
- Reverts the test coverage changes from the previous commit so we test the else case.
- Existing tests of role weights are excercising the if() case.
parent 71a3a3b6
......@@ -363,14 +363,21 @@ protected function preLoad(array &$ids = NULL) {
protected function postLoad(array &$entities) {
$entities_by_class = $this->getEntitiesByClass($entities);
// Invoke entity class specific postLoad() methods.
foreach (array_keys($entities_by_class) as $entity_class) {
// We must pass the full $entities array each time, so that if a
// postLoad() method is trying to reorder the array (for example,
// \Drupal\user\Entity\Role::postLoad()), it can actually manipulate the
// final array, not a subarray.
// Invoke entity class specific postLoad() methods. If there's only a single
// class involved, we want to pass in the original $entities array. For
// example, \Drupal\user\Entity\Role::postLoad() sorts the array to enforce
// role weights. We have to let it manipulate the final array, not a
// subarray. However if there are multiple bundle classes involved, we only
// want to pass each one the entities that match.
if (count($entities_by_class) === 1) {
$entity_class = array_key_first($entities_by_class);
$entity_class::postLoad($this, $entities);
}
else {
foreach ($entities_by_class as $entity_class => &$items) {
$entity_class::postLoad($this, $items);
}
}
// Call hook_entity_load().
foreach ($this->moduleHandler()->getImplementations('entity_load') as $module) {
$function = $module . '_entity_load';
......
......@@ -146,11 +146,11 @@ public function testEntitySubclass() {
$this->assertEquals(2, EntityTestBundleClass::$postLoadCount);
$this->assertCount(2, EntityTestBundleClass::$postLoadEntitiesCount);
// Only 4 of the 5 requested entities need to be loaded. The other ($entity)
// has already been loaded and we're getting the cached entity without
// re-invoking postLoad(). So the custom postLoad() method should only have
// been invoked with 4 entities.
$this->assertEquals(4, EntityTestBundleClass::$postLoadEntitiesCount[1]);
// Only 3 of the 5 entities we just loaded use the bundle class. However,
// one of them has already been loaded and we're getting the cached entity
// without re-invoking postLoad(). So the custom postLoad() method should
// only have been invoked with 2 entities.
$this->assertEquals(2, EntityTestBundleClass::$postLoadEntitiesCount[1]);
// Reset the storage cache and try loading again.
$this->storage->resetCache();
......@@ -158,8 +158,8 @@ public function testEntitySubclass() {
$entities = $this->storage->loadMultiple($entity_ids);
$this->assertEquals(3, EntityTestBundleClass::$postLoadCount);
$this->assertCount(3, EntityTestBundleClass::$postLoadEntitiesCount);
// This time, all 5 entities should be included.
$this->assertEquals(5, EntityTestBundleClass::$postLoadEntitiesCount[2]);
// This time, all 3 bundle_class entities should be included.
$this->assertEquals(3, EntityTestBundleClass::$postLoadEntitiesCount[2]);
// Start deleting things and count delete-related method invocations.
$entity_test_1->delete();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment