Session Limit's \Drupal\session_limit\Services\SessionLimit class currently type-hints the default ModuleHandler class instead of the interface. This prevents custom overrides of ModuleHandler, for example by Hux and Hook Event Dispatcher.
Problem/Motivation
Updated Hook Event Dispatcher from 3.2.0 to 3.3.1, now getting fatal errors like this:
The website encountered an unexpected error. Please try again later.
TypeError: Drupal\session_limit\Services\SessionLimit::__construct(): Argument #6 ($moduleHandler) must be of type Drupal\Core\Extension\ModuleHandler, Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler given, called in E:\Web\Omnipedia\Web\drupal\core\lib\Drupal\Component\DependencyInjection\Container.php on line 262 in Drupal\session_limit\Services\SessionLimit->__construct() (line 119 of modules\contrib\session_limit\src\Services\SessionLimit.php).
Drupal\session_limit\Services\SessionLimit->__construct(Object, Object, Object, Object, Object, Object, Object, Object) (Line: 262)
Drupal\Component\DependencyInjection\Container->createService(Array, 'session_limit') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('session_limit') (Line: 136)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.request') (Line: 134)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 49)
Drupal\remove_http_headers\StackMiddleware\RemoveHttpHeadersMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Steps to reproduce
Install Hook Event Dispatcher 3.3.0+ after installing any module that injects the module_handler service with the parameter being type-hinted to the core ModuleHandler class instead of ModuleHandlerInterface. This should result in a similar fatal error to the above. The module in the error is Session Limit, and the error causing parameter can be found in Services/SessionLimit::__construct()
Proposed resolution
Change type-hint from the default service implementation \Drupal\Core\Extension\ModuleHandler to the interface \Drupal\Core\Extension\ModuleHandlerInterface.
Remaining tasks
Make the changes above.
User interface changes
None.
API changes
Unsure?
Data model changes
None?
| Comment | File | Size | Author |
|---|
Issue fork session_limit-3308506
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:
Comments
Comment #2
ambient.impactBy the way, fatal error aside, the work in #3277301: Adapt to centralised hook dispatcher in Drupal 9.4 is really neat and awesome to see moving forward. I suggested decorating the module handler service a couple of years ago to @pdenooijer but implementing it at the time was more significantly more difficult due to core not being in the place it is now.
Comment #3
el7cosmosIndeed, it is easier because of the changes in 9.4.
IMO this should be changed in the Session Limit itself, type hinting to an interface rather than implementation is better, also there are other projects that decorate the module handler.
Comment #4
benaboki commentedUpgrading to 3.3 from 3.2 is also giving me errors. I fixed some by patching facets (https://www.drupal.org/project/facets/issues/3308343). This replaces ModuleHandler with ModuleHandlerInterface.
Comment #5
benaboki commentedComment #6
svendecabooterSame problem occurs in combination with the easy_breadcrumb module.
Patch to fix this in that module: https://www.drupal.org/project/easy_breadcrumb/issues/3307333
Comment #7
dpiCrosslinking a variety of issues mentioned here.
Comment #8
dpiThis is a problem with Session Limit. The project should be typehinting the interface, not the concrete implementation.
This would defeat one of the reasons for having service interfaces. There should not be a need to extend any Core service, I recommend mentally assuming most core services are marked
finalin order to avoid this kind of issue.---
Moving to the appropriate queue and reformatting IS.
Comment #9
dpiComment #10
ambient.impactYou're right. I was in a bit of a rush, so didn't look too closely at Hook Event Dispatcher class, which does in fact implement
\Drupal\Core\Extension\ModuleHandlerInterfaceas is best practice.Comment #11
ambient.impactCleaned up issue summary, and rephrased to reflect the issue being moved to the Session Limit issue queue.
Comment #12
ambient.impactComment #13
ambient.impactLooks like there are a bunch of dependencies in
Services\SessionLimitthat are type-hinted to the core classes rather than interfaces. I can change the scope of this issue to fix those too if you all are alright with it.Comment #14
ambient.impactComment #16
jpoesen commentedConfirming that the patch in #15 (MR 5) fixes the problem.
Thanks, @Ambient.Impact!
Environment:
- Drupal 9.4.5
- session_limit 2.0.0-beta2
Comment #17
igork96 commentedHere is a patch for fixing the issue. Please review it.
Comment #18
alina.basarabeanu commentedUsing
- hook_event_dispatcher 4.0.0-beta1
- Drupal core 9.5.9
- session_limit 2.0.0-beta3
I get the following error:
Argument #6 ($moduleHandler) must be of type Drupal\Core\Extension\ModuleHandler, Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler given, called in /var/www/html/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259I've applied the patch from #17 and the error is fixed.
Comment #19
fskreuz commentedUpdated patch to change all the dependencies to use interfaces. Problems will occur eventually with the others, makes sense to fix them all in one go.
Comment #20
k3vin_nl commentedConfirming the latest patch #19 works for us.
Comment #21
nimoatwoodwayConfirming the latest patch #19 works for us.
Comment #22
fskreuz commentedReroll of #19
Comment #23
lisotton commentedI applied the #22 and it worked fine with Hook Event Dispatcher.
Moving it to RTBC.
Comment #24
vladimirausLooks like partial duplicate of #3361326: Fix the issues reported by phpcs and phpstan with no proper MR.
Comment #25
fskreuz commentedRe-opening with updated patch. https://www.drupal.org/project/session_limit/issues/3361326 missed a few things from this issue.
Comment #26
vladimirausPlease use merge request for easier review process. Thanks.
Comment #30
lisotton commentedI have opened the MR.
Comment #31
vladimirausThank you! Looks good. 🍻
RTBC
Comment #32
bdanin commentedConfirmed the MR fixes the issue for me as well.
Comment #34
rajivgandhi chinnakrishnan commentedSession limit 2.0.1 is getting conflicted with hook_event_subscriber module. This patch resolves the conflict.
Comment #35
fskreuz commentedThe MR already covers the replacing of
ModuleHandlertoModuleHandlerInterface, and more. Hiding this patch to avoid confusion.Comment #36
jannakha commented+1 RTBC
Comment #38
vladimirausDoneski! Thank you! 🥂