Problem/Motivation
Under certain circumstances the module files are not yet loaded.
This happens for example when invoking a hook inside the constructor of a http_middleware service; theses services are constructed very early as a dependency of http_kernel service.
A more concrete example is a middleware service using the entity_type.manager. Most of the times the entity type information is retrieved from cache (stored in the discovery cache bin). When this cache however is missing, hooks like hook_entity_type_build() and hook_entity_type_alter() need to be invoked at this early stage. The modules files however haven't been loaded yet, making ModuleHanlder::verifyImplementations() skip all the hook implementations (silently!). The entity definition cache is then incomplete resulting in various hard to debug (next to impossible) exceptions like #3031598: The "paragraph" entity type did not specify a translation handler. .
Steps to reproduce
Taken from this comment.
- Just uninstalled the dblog module from UI here /admin/modules/uninstall
- Then open any menu /admin/structure/menu/manage/[menu-name] and I see an error:
Uncaught PHP Exception Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: "The "menu" entity type did not specify a "edit" form class." at /core/lib/Drupal/Core/Entity/EntityTypeManager.php line 211 - Then click to Flush views cache on another admin page (Flush all caches > Flush views cache via admin menu).
- After #3 I can see the error:
Uncaught PHP Exception Symfony\Component\Routing\Exception\RouteNotFoundException: "Route "entity.path_alias.collection" does not exist." at /core/lib/Drupal/Core/Routing/RouteProvider.php line 206
Why does it happen?
After #3, a lot of items were removed from router table from the database. You have to launch drush cr twice to fix such error.
Proposed resolution
If a hook is invoked before the modules being loaded, load all modules.
Remaining tasks
Update issue summary
Address #12 and #17