Currently the hook cache is an array of the format $module_name => $group.
We also know that the hook system is fragile.
sun suggested to allow an alternative format with Drupal\$module\Hook::foo() (and other proposals existed before that):
#2237831: Allow module services to specify hooks
As a preparational step before any such move, I suggest to change the hook cache so that it can be filled with arbitrary callbacks.
For the start, these can be added with hook_module_implements_alter().
Later we can add patterns like Drupal\$module\Hook::foo() or Drupal\$module\hook_foo(). This could even happen post-release, since it would only extend the API and not break it. (unless a module already defines a class that matches the pattern.. duh)
The change of the hook cache brings only one API break: It changes the structure of hook_module_implements_alter().
hook_hook_info() will continue to work as it does today.
Challenge: We still need a way to store which files should be included for a hook implementation. We may want to store the module name and the file path relative to the module dir. This way, things don't break if modules are moved around.
This means the simple array structure of $module_name => $group no longer does the job.
Note: #2263365: Second loop in module_implements() being repeated for no reason. should be fixed first!
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | 2299537-nr-bot.txt | 144 bytes | needs-review-queue-bot |
| #16 | 2299537-16.patch | 7.02 KB | shaktik |
| #8 | 2299537-2-hook_classes.patch | 6.12 KB | chi |
Comments
Comment #7
chi commentedThe attached patch allows to define hooks in classes like follows.
Note that the hook callback is not a static method. So it is possible to inject dependencies via factory
::create()method. Current implementation is extremely simple and does not cover all edge cases. I expect tests will fail because there are a few places where hooks are invoked without using module handler service (example).Comment #8
chi commentedComment #9
chi commentedThe patch only supports hook that invoked through module handler.
Comment #10
chi commentedThe implementation would be much easier without
hook_hook_info().Comment #12
andypostToo late for 8.9
Comment #13
joachim commented> Later we can add patterns like Drupal\$module\Hook::foo() or
This change looks like that proposal, which the issue summary says is not being tackled here.
Shouldn't this use the ClassResolver instead?
Also,
> The change of the hook cache brings only one API break: It changes the structure of hook_module_implements_alter().
The patch doesn't have any change to docs which an API change would need.
Though I think it would be better if we can maintain that structure if possible.
Comment #14
xjmThis would be a minor-only change. Since 8.9.x and 9.0.x are now in beta, I'm moving this to 9.1.x. Thanks!
Comment #15
shaktik#8 patch does not apply success on D9.
I am working on this.
Comment #16
shaktikComment #18
shaktikTestcase is passed on 7.3 and 7.4 kindly review.
Comment #19
Rkumar commentedUnassinging it for review.
Comment #25
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #26
larowlanShould we close won't fix this in favour of something like drupal.org/project/hux
Comment #27
andypostOTOH it makes sense to re-profile invocations as since PHP 8.1 new callable (faster calls) added so there's issues like #329012: Remove call_user_func_array() from ModuleHandler::invoke() + invokeAll() and #3259716: Replace usages of static::class . '::methodName' to first-class callable syntax static::method(...)
Comment #29
donquixote commentedContinue here, #3366083: [META] Hooks via attributes on service methods (hux style)
Comment #30
nicxvan commented@donquixote, can this be closed?
Comment #31
nicxvan commentedPretty sure it can be it was already on the cusp and the issue referenced was also closed due to
#3442009: OOP hooks using attributes and event dispatcher
Comment #32
nicxvan commented