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

postLoad() hook not called correctly in SqlContentEntityStorageTrait #8

Open
simonbaese opened this issue Oct 25, 2018 · 2 comments · May be fixed by #9
Open

postLoad() hook not called correctly in SqlContentEntityStorageTrait #8

simonbaese opened this issue Oct 25, 2018 · 2 comments · May be fixed by #9

Comments

@simonbaese
Copy link

Current implementation

protected function postLoad(array &$entities) {
    $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';
      $function($entities, $this->entityTypeId);
    }
    // Call hook_TYPE_load().
    foreach ($this->moduleHandler()
               ->getImplementations($this->entityTypeId . '_load') as $module) {
      $function = $module . '_' . $this->entityTypeId . '_load';
      $function($entities);
    }
  }

does not determine entity_class of used entity bundle, but rather of the given parent entity, as there is no parameter given to getEntityClass().

I suggest to invoke the postLoad hook by bundle. Maybe like following.

protected function postLoad(array &$entities) {
    // We have to determine the bundle first.
    $bundle = FALSE;
    if (!empty($entities) && isset($this->bundleKey)) {
      $entity = reset($entities);
      $bundle = $entity->get($this->bundleKey)->getString();
    }
    $entity_class = $this->getEntityClass($bundle);
    $entity_class::postLoad($this, $entities);
    // Call hook_entity_load().
    foreach ($this->moduleHandler()
               ->getImplementations('entity_load') as $module) {
      $function = $module . '_entity_load';
      $function($entities, $this->entityTypeId);
    }
    // Call hook_TYPE_load().
    foreach ($this->moduleHandler()
               ->getImplementations($this->entityTypeId . '_load') as $module) {
      $function = $module . '_' . $this->entityTypeId . '_load';
      $function($entities);
    }
  }
@amcgowanca
Copy link
Owner

@simonbaese Thanks for submitting this! I haven't take a deep enough look, but the intent here is excellent. We should explore getting a PR open for this and merged as I do see the benefits :)

simonbaese added a commit to simonbaese/discoverable_entity_bundle_classes that referenced this issue Oct 29, 2018
@simonbaese simonbaese linked a pull request Oct 29, 2018 that will close this issue
@weitzman
Copy link

weitzman commented Sep 3, 2020

@amcgowanca looks like you got 2 PRs for this. Still want to merge it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants