Problem/Motivation
There are some inconsistencies between how local tasks and local actions are handled.
For local tasks, there are 2 hooks:
hook_menu_local_tasks_alter()- this allows local tasks to be altered prior to renderinghook_local_tasks_alter()- this allows the altering of local task plugins during discovery
For local actions, there is only one hook, and the naming convention is actually the opposite of local tasks:
hook_menu_local_actions_alter()- allows altering of local action plugins during discovery
Proposed resolution
Introduce a new hook_local_actions_render_alter() executed prior to rendering.
Rename existing hook_menu_local_tasks_alter() to hook_local_tasks_render_alter() for the consistency.
Remaining tasks
Review, commit.
API changes
New hook_local_actions_render_alter() hook is added.
Existing hook_menu_local_tasks_alter() hook is deprecated and renamed to hook_local_tasks_render_alter().
See the change record for more details - https://www.drupal.org/node/3028611.
| Comment | File | Size | Author |
|---|
Issue fork drupal-2585169
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jhedstromHere is an initial patch. It will need tests eventually.
Comment #3
jrockowitz commentedI definitely experienced a WTF moment with these hook naming conventions. I want to alter local actions labels and url when they are being rendered, so that I can add more contextual information like 'Add content to {this page}'.
Comment #4
dawehnerWow yeah that is quite sad :(
So the runtime vs. non runtime hook is completely opposite, not sure how to deal with that at all.
Comment #6
dawehner@jhedstrom
Do you think we can provide a little test to ensure this actually works?
Comment #7
jhedstrom@dawehner sure!
Comment #8
jhedstromHere's a test. The interdiff is the same as the test-only patch.
Comment #13
farald commentedLocation of test has changed.
Comment #14
sathish.redcrackle commentedUploading a test-only patch so we can see the test fails for drupal 8.5.x.
Comment #16
sathish.redcrackle commentedComment #18
harsha012 commentedComment #20
metallized commentedComment #21
k4v commentednevermind.
Comment #22
k4v commentedI tested this patch fine in Drupal 8.5.4. This allowed me to work around the core bug mentioned in
https://www.drupal.org/project/drupal/issues/2722237
It would be nice to have this commited.
Comment #23
k4v commentedComment #24
xjmThis seems like a reasonable API addition. Thanks @k4v for testing the patch!
If there is a
@todoin the code it should have a followup issue that is included in the code comment. :) Also note the formatting in https://www.drupal.org/node/1354#todo.If we plan to change
hook_local_actions_alter()in D9, there should probably be a deprecation: https://www.drupal.org/core/deprecation#how-hook I'm not totally sure of what exactly we want to do but an explicit deprecation and followup should help. :)Also, we should have a change record for this change. Thanks!
Comment #26
webflo commentedI wrote a change record. https://www.drupal.org/node/3028611
Comment #27
webflo commentedI've added a link/issue to the todo.
Comment #28
tstoecklerLooks good to me! Thanks
Comment #29
webflo commentedComment #30
knyshuk.vova commented+1 to RTBC
Comment #31
alexpottAs far as I can see we're adding a new hook for render altering called
hook_local_actions_alter().Reading the issue summary this means we'll have:
Which is exactly the opposite of local tasks - according to the issue summary.
This will be really confusing for developers - also there is no way to deprecate to swap them around. I suggest we come up with a plan where we can make this more consistent in all cases.
Comment #32
alexpottI suggest doing something like this...
Actions
Discovery hook: hook_menu_local_actions_alter -> hook_local_actions_alter - need an issue to deprecate the old hook and implement the new one.
Render hook: does not exist -> hook_local_actions_render_alter - add that here
Tasks:
Discovery hook: hook_local_tasks_alter ->hook_local_tasks_alter - stays the same yay!
Render hook: hook_menu_local_tasks_alter -> hook_local_tasks_render_alter - deprecate hook_menu_local_tasks_alter here and add hook_local_tasks_render_alter for consistency.
Comment #33
alexpottWe could choose to do the entirety of #32 and make this issue about making tasks and action hooks consistent.
Comment #41
matroskeen1. Opened a merge request with the changes from the latest patch and minor improvements to hook example and documentation.
2. Moved to "Needs review" two related issues responsible for renaming existing hooks as suggested by @alexpott in #32: #3216564: Rename hook_menu_local_tasks_alter to hook_local_tasks_render_alter and #3028616: Rename hook_menu_local_actions_alter() to hook_local_actions_alter().
3. Updated issue summary and change record - https://www.drupal.org/node/3028611.
Would love to see this in 9.3.x :)
Comment #42
matroskeenI'm merging #3216564: Rename hook_menu_local_tasks_alter to hook_local_tasks_render_alter into this task to make sure we keep those two hooks consistent.
It was suggested by @alexpott in Slack when I asked him for review:
Comment #44
duaelfrPatch is working well on my project.
Thanks a lot!
Comment #47
smustgrave commentedSame as #3028616: Rename hook_menu_local_actions_alter() to hook_local_actions_alter() can we update the MR for 10.1 please
Comment #48
jonmcl commentedWorking well for us. We are using this to set a specific route parameter based on external information:
A word to the wise: don't be like me and remember that you may likely need to have a method of invalidating the cache for this rendered item.
Comment #51
rpayanmPlease review.
Comment #52
smustgrave commentedAppears this was just a copy but not updated for 10.1 but should be simple.
Comment #56
duaelfrI rerolled !3858 into a new MR based on 11.x.
I converted procedural hooks to object ones.
Attached: patches for composer
Comment #57
andypostversion needs update to 11.2=>12.0
Comment #58
duaelfr@andypost Feedback addressed, thanks.
Comment #59
andypostAdded suggestion to format deprecation message (link should not have dot at the end)
And it needs deprecation test to make sure backward compatibility works but throws deprecation
Comment #60
berliner commentedI don't have time to look at the MRs and update them, but I have re-rolled https://git.drupalcode.org/project/drupal/-/merge_requests/744 for 10.3.x. I'll upload that as a patch file for now.
Comment #61
berliner commentedDidn't meant to drop the last changes.
Comment #62
duaelfr@andypost I accepted your suggestion an rebased the branch on 11.X
I have no idea how to write the deprecation test, though. Would you guide me, please?
Comment #63
duaelfrHere are updated patches for composer
Comment #64
oily commented@duaelfr Regarding the deprecation test: https://www.drupal.org/about/core/policies/core-change-policies/how-to-d.... You can search recursively through the core folder for the string '@Group legacy' to find existing deprecation tests.
Comment #66
oily commentedFixed Unit test in MR!10284.
Pipeline tests all green.
Test coverage seems to be in place for the hook in MenuTestHooks.php.
We still need a deprecation test.
Comment #67
oily commentedComment #68
duaelfrPatch for composer with latest changes