Opened 4 years ago
Last modified 20 months ago
#1378 new Defect
register_plugin_hook priority isn't applied globally
| Reported by: | twall | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Long Term Future Release |
| Component: | Core | Version: | 1.6 |
| Severity: | minor | Keywords: | |
| Cc: | brettp | Difficulty: |
Description
The priority passed in with register_plugin_hook only affects the hook's priority in relation to other hooks registered with the same hook/object type.
To ensure a hook is run first, you must both specify a priority of '0' AND specify both the hook and entity type. To ensure a hook is run last, you must specify a priority of 1000 and use 'all'/'all' as the hook/object type. Using hook names and entity types other than what is appropriate, simply to affect the priority, is what makes me consider this a bug.
The following sets of hooks are executed in order:
'hook'/'type'
'all'/'type'
'hook'/'all'
'all'/'all'
It may not be possible to merge these sets after registration, since the original ordering information is no longer available. However, executing as they do results in the wrong results for priority.
Specificly, my 'check_permissions'/'object' hook will always be overridden by the 'check_permissions'/'all' hook set up by someone else, regardless of priority.
Change History (3)
comment:1 Changed 4 years ago by cash
- Priority changed from major to minor
comment:2 Changed 3 years ago by brettp
- Milestone changed from Elgg 1.7 to Elgg 2.0
- Type changed from unconfirmed defect to confirmed defect
Because the order of execution (however odd) can be absolutely determined, I'm holding this one until 2.0. At that time we can re-evaluate the event priority system for better order of execution.
comment:3 Changed 20 months ago by cash
- Milestone changed from Elgg 2.0 to Future Release

I don't see anyway to do this differently. Right now, order of hook execution is predictable exactly as described above. Any effort to combine the 4 specificities into a single array of hooks on execution will
If you want to guarantee that your hook runs last: register for all/all with the lowest priority and filter on hook and type so it only runs for the specific hook you are interested in.
Any attempt to rewrite the code to make it easier to register for the last hook will likely make it more difficult to do other things or make things unnecessarily complicated.
I'll leave this open for Brett to look at.