From 72b03315715d925e45a185ed0e3c0c1e276b6d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=A4se?= Date: Mon, 29 Oct 2018 15:24:17 +0100 Subject: [PATCH] Call postLoad() hook by bundle #8 --- src/Storage/SqlContentEntityStorageTrait.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Storage/SqlContentEntityStorageTrait.php b/src/Storage/SqlContentEntityStorageTrait.php index 50899e4..e2ab56a 100644 --- a/src/Storage/SqlContentEntityStorageTrait.php +++ b/src/Storage/SqlContentEntityStorageTrait.php @@ -121,8 +121,24 @@ protected function mapFromStorageRecords(array $records, $load_from_revision = F * {@inheritdoc} */ protected function postLoad(array &$entities) { - $entity_class = $this->getEntityClass(); - $entity_class::postLoad($this, $entities); + // Call postLoad() by bundle if bundleKey is set. + // Iterate over entities to determine respective bundles because entities + // might are of different types. + if (isset($this->bundleKey)) { + $entities_by_bundle = []; + foreach ($entities as $entity) { + $entities_by_bundle[$entity->get($this->bundleKey)->getString()][] = $entity; + } + foreach ($entities_by_bundle as $bundle => $entities_bundled) { + $entity_class = $this->getEntityClass($bundle); + $entity_class::postLoad($this, $entities_bundled); + } + } + // Call postLoad() by parent entity if it is not bundled. + else { + $entity_class = $this->getEntityClass(); + $entity_class::postLoad($this, $entities); + } // Call hook_entity_load(). foreach ($this->moduleHandler()->getImplementations('entity_load') as $module) { $function = $module . '_entity_load';