Problem/Motivation
We should not perform any logic (like retrieving entity storage handlers) while constructing services. The constructors are executed when the dependency injection container is built and this can have unintended side effects.
I'm getting a fatal error when doing a clean install using existing config: Call to undefined function \paragraphs_entity_type_alter() which can be traced back to the instantiation of the Masquerade class during container building. It tries to get the user storage from the entity type manager, which calls EntityTypeManager->findDefinitions() which in turn calls DefaultPluginManager->alterDefinitions() which finally throws the fatal error since this code is not functional with a half built DI container.
Moral of the story: do not call code in constructors that has the potential to fire hooks.
Steps to reproduce
This can be quite tricky to reproduce. I get it when installing a project from existing config. I did not narrow this down to a minimal set of modules.
Proposed resolution
Do not perform logic when constructing services.
Issue fork masquerade-3572640
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3572640-avoid-logic-in-constructors
changes, plain diff MR !30
Comments
Comment #2
andypostlooks like good idea
Comment #4
pfrenssenThanks! I have made the change. The test failure is unrelated.
Comment #5
andypostLooks that's only place to fix, thank you!
Gonna merge later this week
Comment #7
andypostMerged, thank you
Comment #9
mkalkbrennerYou should consider a new release quickly. Masquerade breaks commerce 3.3.
Comment #10
andypostpublished https://www.drupal.org/project/masquerade/releases/8.x-2.1