Objective
-
#1892574: Remove hook_hook_info_alter() was removed a long time ago already.
-
hook_hook_info()
only exists for lazy-loading legacy hook implementation code. -
hook_hook_info()
has architectural problems causing code to not be loaded (see related issues). -
ModuleHandler
apparently maintains two cache items; one for'module_implements'
, and another one just for'hook_info'
. -
Object-oriented code can be properly auto-loaded in D8.
Remaining implementations
- system_hook_info() (hook_token*())
- views_hook_info() (hook_views_*())
Proposed solution
-
Remove
hook_hook_info()
+ persistent caching fromModuleHandler
. -
Convert remaining affected hook implementations...
A) Either move them into .module files.
B) Or find a simple object-oriented replacement pattern.
Comments
Comment #1
sunClarifying summary that ModuleHandler maintains a separate cache item for hook_info.
Comment #2
sunCreated #2233353: Convert Token API into plugins
Comment #3
tim.plunkettPlease have a look at views_hook_info().
None of that is legacy. Until something like #1972304: Add a HookEvent or #2043653: Allow modules to implement hooks by nominating methods in a module.implements.yml goes in, I don't see how or why we should move forward here.
Comment #4
catch#1443308-97: Add static cache to module_load_include() shows that views_hook_info() is a net negative for performance. With an opcode cache, having the hook implementations in the module file has a negligible impact, vs. the discovery which is non-negligible.
Comment #5
catchAlso we can't nuke this until 9.x unfortunately, but we can discourage it before then.
Comment #6
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedI like the idea of killing it, although it would be nice for some of the code organization benefits that it provides to be retained (and in fact expanded).
But I guess that can still be done with require_once statements at the top of the .module file...
Comment #9
dpiShould we issue a deprecation notice?
Comment #10
joelpittet@dpi That sounds good but
8.3.x
or8.4.x
would be the start were it was deprecated.Comment #12
Mile23We're trying to figure out how to @trigger_error() for deprecated hooks here: #2870058: [policy, no patch] Document how to deprecate hooks
Comment #17
andypostPolicy accepted https://www.drupal.org/core/deprecation#how-hook
Still no replacement for the hook
Comment #19
donquixote CreditAttribution: donquixote commentedI don't understand why this is being suggested.
If we replace hooks completely, fine. But we are not there yet I think.
Until then, something like hook_hook_info() is useful for code organization, similar to autoloading for classes (not autoloading itself, but the PSR-4 file organization that is made necessary by autoloading).
To me, performance is not the main reason for not putting everything into the main *.module file.
Yes. Code organization +1.
Yes, but:
The issue summary contains some more arguments, which are quite old.
I don't know if these are still valid.
As long as we have hooks, it would be useful to be able to force a convention for file organization per hook.
I am not married to hook_hook_info() specifically, but we should find more convincing reasons to remove it, and/or suggest a good replacement.
Comment #26
andypostRemoved
field_hook_info()
from IS as it was moved toviews_hook_info()
in #2399931: Generic entity api field handler should live in views module not in field moduleSo only tokens and views using this hook but it needs to deprecate
\Drupal\Core\Extension\ModuleHandlerInterface::getHookInfo()
as wellFiled follow-up to remove broken ref #3346614: Remove stray reference to field_hook_info()
Comment #27
andypostClosed as duplicates
- #1373884: [meta] Core does not use hook_hook_info()
- #1107528: Stop using magic functions and force modules to declare the hooks they want to implement
- #1029158: Let hook_hook_info() handle hooks in .install files (aka "rename MODULE.install to MODULE.install.inc")
Comment #28
catchJust to answer #19 - a lot of modules and some of core too already puts the logic of their hook implementations into classes, so that the hook implementation in .module is just three lines or so. This pattern is available for every hook, not just the ones that have hook_hook_info() implemented for them.