Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call postLoad() hook by bundle #8 #9

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Storage/SqlContentEntityStorageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonbaese Could we not use something like just $entity->bundle()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amcgowanca I was not really sure because you were checking for the bundleKey throughout the code. The method bundle() specifically looks for the value by the key 'bundle' in the entity (see ContentEntityBase) and as it is public it could also be overwritten.

}
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';
Expand Down