Drupal 8 is almost fully object-orientiered everywhere except of hooks. In the other hand it is still rather flexible tool. A lot of core modules now use .module file just only for collecting information about hooks, delegating other classes, as usual injectable, hooks implementation. In order to implement standard oop approach here, it would be nice to have hook plugin manager, that collects hooks implementations. As alternative this can be service collector or some event (symfony approach), but plugin would suite the best.
Comments
Comment #2
olexyy.mails@gmail.com commentedThat would be the first step to deprecating static calls of services. As for manager itself, I see two approaches.
First: each plugin could have machine name according to hook naming convention and only one method to be called. So plugin would be picked up by machine name. In result each hook would have one file.
Second approach to have this method not for implementation, but for information of hooks implementation, so several hooks could be implemented in one plugin.
Comment #3
cilefen commentedHi! This is an interesting idea however it seems to be duplicating existing efforts.
Comment #4
olexyy.mails@gmail.com commentedHere we can discuss plugins implementation, other issues about tagged service and events.
Comment #5
olexyy.mails@gmail.com commentedI've just started basic implementation as contrib by altering module_handler service.
Current implementation has some advantages:
- it should be rather fast;
- it uses caching and static;
- priority is supported;
- we have progress in naming convention problems;
- we can inject services to classes that implement hooks;
Repo:
https://github.com/Olexyy/hook_manager.git
If there are any thoughts, please share.
Comment #6
olexyy.mails@gmail.com commentedRelated working project.
https://www.drupal.org/sandbox/olexyymailsgmailcom/hook_manager
There is now added 'static' map for alter hooks, changed priority logic.
This can be merged with core, if in general is ok and performance is enough.
Comment #7
olexyy.mails@gmail.com commentedThe idea is rather simple:
Annotation to plugin contains list of canonical names for hooks with corresponding priority. (f.e. hook_theme => 0)
We need to implement this hook as method in camel case, so this is public function hookTheme();
Comment #18
catchDuplicate of #3442009: OOP hooks using attributes and event dispatcher