Status: Waiting for peer review
This is mostly covered by tests.
The Hooks overview page can be tested manually after we convert some patch, for now you can use https://github.com/skodak/moodle/tree/MDL-74954_examples
In general lib.php callbacks used via plugin_callback() and get_plugins_with_function() have multiple known problems:
- lack of documentation - list of callbacks is not maintained, the parameters are not documented properly
- unnecessary memory use caused by inclusion of lib.php files
- potential naming conflicts when adding new callbacks, this limits adding of callbacks in stable branches
- hardcoded support for limited number of plugin types in some areas
- poor backwards/forwards compatibility - callback function signature cannot be changed easily
- plugin callbacks cannot be used during installation and upgrade which prevents their use in some low level APIs
- it is hard to differentiate purpose of different lib.php functions - one-to-many or one-to-one callback; plugin APIs; local customisation callbacks, etc.
Hooks were already proposed in
MDL-44078 as a replacement for some lib.php callbacks, unfortunately they were not accepted. I'd like to ask HQ developers to reconsider inclusion of hooks into Moodle codebase. One of the reasons was the lack of standards, this proposal implements hooks using PSR-14.
For more information about current one-to-many callbacks see https://docs.moodle.org/dev/Callbacks. Please note this proposal is not relevant to on-to-one callbacks that are also defined in lib.php files, also some one-to-many callbacks describing features of one particular plugin type are not be suitable candidates for conversion to hooks either.
Hooks are similar to standard events, the main difference is that hooks allow two-way communication between 2 or more different components.
The architecture is based on PSR-14 recommendation.
Hook class defines the data structure that is used for communication between plugins, hook instance can be modified by any registered callback. Hooks are mutable.
Hook classes are stored in \any_plugin\hook\ namespace, hook callbacks are registered by adding db/hooks.php files.
Admin page with hooks overview:
Hook callback registration example:
Each hook may contain a list of lib.php plugin callbacks that are deprecated by the hook.
Functions plugin_callback() and get_plugins_with_function() may be asked to lookup if callback is deprecated and if the called component already implements the new hook callback and prints deprecation message or ignored the old plugin callback. The options are:
- lib.php plugin callback was not replaced by any hook - nothing changes and everything works as before for now
- lib.php plugin callback was marked as deprecated by some hook, the plugin with callback does not use the new hook yet - deprecation message is printed and old callback is called
- lib.php plugin callback was marked as deprecated by some hook, a plugin implements both the old lib.php callback and registered a new hook callback - new hook callback is called, old lib.php callback is ignored
- Based on PSR-14.
- Any plugin type can register as callback for any hook.
- All available hooks in system are listed on new admin page.
- Optimal memory use when executing hooks due to class autoloading.
- Hook data can be altered while keeping backwards/forwards compatibility.
- Hooks may contain utility methods, not just data.
- Hooks can be used during installation and upgrades.
- Hooks can be used in PHPUnit internals - such as for static variables reset callbacks.
- Hooks may implement stoppable interface.
Events are triggered after something happens - hooks can be used to decide if something is allowed to happen, they can be used to alter how things happen, other plugins can be notified before something happens.
All events are logged - hooks are not logged anywhere.
Event data cannot be modified - hook data can be modified from callbacks returning data back to where the hook was dispatched.
Events are not reliable during installation and upgrades - hooks can be used during installation and upgrade.