Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
register_plugin_hook priority isn't applied globally (Trac #1378) #1378
Original ticket http://trac.elgg.org/ticket/1378 on 39884583-04-02 by trac user twall, assigned to unknown.
Elgg version: 1.6
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:
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.
cash wrote on 39886573-03-29
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.
This was referenced
Feb 16, 2013
I seem to run into this every few months... registering for 'all/all' to ensure your hook runs last is all well and good, but now I need my hook to run first.
I can hook into 'route', 'all' - but then I can't ensure this hook runs first as any more specific hook runs first regardless of the priority I set.
I can parse all of my necessary page handlers first, and register the same hook for them individually in a more specific manner, but that seems messier to me. My opinion is that priority should be honored.
I suggest handlers be stored under the key
On trigger, you merge all the hooks/types you want into a new temp var, sort by keys (priority) then walk through each priority, then sort the subarray by keys (registration order) before walking through it.
In effect all handlers are on an even playing field, and registration order solves disputes over priorities.
Why was this removed from 2.0.0?
Ran into this again...
I have a hook on 'route,all' that needs to trigger first and does some security checks and only allows access to specific pagehandlers. In this case it needs to know what is allowed, and disallow everything else. So it can't be registered on specific handlers because we don't know what those are.
What I'm getting at is it's really unintuitive despite the fact that it's technically predictable, and it's problematic. It can't be fixed in 1.x but in 2.0 I want priority to actually be the priority.