Problem/Motivation

There are some inconsistencies between how local tasks and local actions are handled.

For local tasks, there are 2 hooks:

For local actions, there is only one hook, and the naming convention is actually the opposite of local tasks:

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.

Issue fork drupal-2585169

Command icon 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

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new2.25 KB

Here is an initial patch. It will need tests eventually.

jrockowitz’s picture

I 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}'.

dawehner’s picture

Wow yeah that is quite sad :(

So the runtime vs. non runtime hook is completely opposite, not sure how to deal with that at all.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

@jhedstrom
Do you think we can provide a little test to ensure this actually works?

jhedstrom’s picture

Assigned: Unassigned » jhedstrom

@dawehner sure!

jhedstrom’s picture

Assigned: jhedstrom » Unassigned
StatusFileSize
new2.4 KB
new2.4 KB
new4.69 KB

Here's a test. The interdiff is the same as the test-only patch.

The last submitted patch, 8: 2585169-08-TEST-ONLY.patch, failed testing.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

farald’s picture

StatusFileSize
new4.74 KB

Location of test has changed.

sathish.redcrackle’s picture

Uploading a test-only patch so we can see the test fails for drupal 8.5.x.

The last submitted patch, 14: test_only_patch_2585169.patch, failed testing. View results

sathish.redcrackle’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2585169-13-alter-local-actions_0.patch, failed testing. View results

harsha012’s picture

Status: Needs work » Needs review

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

metallized’s picture

Version: 8.5.x-dev » 8.6.x-dev
k4v’s picture

nevermind.

k4v’s picture

I 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.

k4v’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests
xjm’s picture

Category: Bug report » Task
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

This seems like a reasonable API addition. Thanks @k4v for testing the patch!

+++ b/core/lib/Drupal/Core/Menu/LocalActionManager.php
@@ -197,6 +197,11 @@ public function getActionsForRoute($route_appears) {
+    // @todo For consistency with local tasks, this hook should actually be
+    // named hook_menu_local_actions_alter().

If there is a @todo in 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!

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

webflo’s picture

I wrote a change record. https://www.drupal.org/node/3028611

webflo’s picture

Status: Needs work » Needs review
StatusFileSize
new4.87 KB

I've added a link/issue to the todo.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me! Thanks

webflo’s picture

Issue tags: -Needs change record
knyshuk.vova’s picture

+1 to RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

As 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:

  • hook_menu_local_actions_alter() - altering during discovery
  • hook_local_actions_alter() - altering during render

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.

alexpott’s picture

I 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.

alexpott’s picture

We could choose to do the entirety of #32 and make this issue about making tasks and action hooks consistent.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Matroskeen made their first commit to this issue’s fork.

matroskeen’s picture

Issue summary: View changes
Status: Needs work » Needs review

1. 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 :)

matroskeen’s picture

Issue summary: View changes

I'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:

Maybe merging it with https://www.drupal.org/project/drupal/issues/3216564 so at least hook_local_actions_render_alter and hook_local_tasks_render_alter are consistent

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

duaelfr’s picture

Patch is working well on my project.
Thanks a lot!

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
jonmcl’s picture

Working well for us. We are using this to set a specific route parameter based on external information:

      /** @var \Drupal\Core\Url $url */
      $url = &$local_actions['local_action.id']['#link']['url'];
      $url->setRouteParameter('node', $node->id());

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.

rpayanm made their first commit to this issue’s fork.

rpayanm’s picture

Status: Needs work » Needs review

Please review.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Appears this was just a copy but not updated for 10.1 but should be simple.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

duaelfr changed the visibility of the branch 2585169-alter-local-actions-render to hidden.

duaelfr’s picture

Status: Needs work » Needs review
StatusFileSize
new11.32 KB
new11.61 KB

I rerolled !3858 into a new MR based on 11.x.
I converted procedural hooks to object ones.

Attached: patches for composer

andypost’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Menu/LocalTaskManager.php
@@ -361,7 +361,8 @@ public function getLocalTasks($route_name, $level = 0) {
+        $this->moduleHandler->alterDeprecated('Deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use local_tasks_render instead. See https://www.drupal.org/node/3216578.', 'menu_local_tasks', $this->taskData[$route_name], $route_name, $cacheability);

+++ b/core/lib/Drupal/Core/Menu/menu.api.php
@@ -310,6 +310,10 @@ function hook_menu_links_discovered_alter(&$links) {
+ * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use
+ *   hook_local_tasks_render_alter instead.

version needs update to 11.2=>12.0

duaelfr’s picture

Status: Needs work » Needs review

@andypost Feedback addressed, thanks.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +@deprecated, +Needs tests

Added 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

berliner’s picture

Issue tags: -@deprecated, -Needs tests
StatusFileSize
new11.32 KB

I 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.

berliner’s picture

Issue tags: +@deprecated, +Needs tests

Didn't meant to drop the last changes.

duaelfr’s picture

@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?

duaelfr’s picture

StatusFileSize
new11.63 KB
new11.35 KB

Here are updated patches for composer

oily’s picture

@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.

oily changed the visibility of the branch 2585169-10.1.x to hidden.

oily’s picture

Fixed 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.

oily’s picture

Issue tags: -Needs tests
duaelfr’s picture

StatusFileSize
new18.68 KB

Patch for composer with latest changes

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.