Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is very similar to #2921844: Allow auto-generating local actions for entities but for local tasks.
The "default" local task provider checks whether there are routes for "View", "Edit" and "Delete" (in that order) and if there are at least two of those, it adds a set of local tasks with the base route of the first one found. That way it can be used for both entities with and without a "View" path (i.e. config entities).
Other than that I really stole everything from that issue including the title-translation trick and the (basic) test.
Comment | File | Size | Author |
---|---|---|---|
#16 | 2994349-16.patch | 14.08 KB | tstoeckler |
|
Comments
Comment #2
tstoecklerHere it is.
Comment #3
tstoecklerOh right: One thing that is weird is the current "Revisions" Task that automatically gets added to all entity types. It's kind of unfortunate to have two different methods of adding tasks to entities, but I guess it would be a BC-break to convert this to a local task provider because that would inherently make it opt-in.
I mean theoretically we could also add an appropriate local task provider in
hook_entity_type_build()
for the revisions task, but not sure...Comment #4
tstoecklerComment #5
bojanz CreditAttribution: bojanz at Centarro commentedLet's do this.
Comment #6
bojanz CreditAttribution: bojanz at Centarro commentedMeant to post that in #2989556: Make EntityCollectionLocalActionProvider add a destination query parameter.
Regarding #3, could we just merge RevisionsOverviewDeriver into EntityLocalTask?
Comment #7
tstoecklerOK, so something like this? I think this would certainly be the cleanest solution. On the one hand people might not be happy about their Revisions tab disappearing, on the other hand, we can tell them "Hey, add these 3 lines to your annotation and you can delete *all* of your local tasks" so...
I renamed the provider, somehow I couldn't get that to show up properly in the interdiff.
Comment #9
tstoecklerOops, I'm not quite up-to-date, it seems ;-)
Comment #11
tstoecklerAnd I guess a bit trigger-happy today. Sorry for the noise.
Not sure what to do with the RevisionOverviewTest. Removing it for now, not sure what others think.
Comment #12
mjpa CreditAttribution: mjpa commentedFound this patch while trying to work out how to stop this module from enforcing the revisions local task... works fine for that bit :D
Comment #13
bojanz CreditAttribution: bojanz at Centarro commentedI'm fine with the approach to the version-history, but I'm confused by the EntityLocalTask class.
It feels very unusual for a plugin class to hardcode every possible title
What's wrong with using t() in the DefaultEntityTasksProvider?
Another unusual thing is that we provide a local task for version-history, but we don't provide a route for it.
Comment #14
tstoecklerAs far as I know the local tasks are cached in a way that running the translation at build time would not result in the proper runtime translation being applied. I did not investigate this deeply, though, I just copied the pattern from what we do with route providers, where it is actually necessary to do this. Will test and investigate though, to see if we can avoid this.
Sorry, I don't understand. As far as I can tell the route is generated by
\Drupal\entity\Routing\RevisionRouteProvider::getRevisionHistoryRoute()
.Comment #15
morenstratI applied the patch, added the local_task_provider handler to my custom entity type and the local tasks appeared.
Comment #16
tstoecklerSo I looked into the translation thing and it is indeed not necessary because local tasks are cached by langcode. See
LocalTaskManager::__construct()
:So here's a simplified patch. The interdiff doesn't show the removed
EntityLocalTask
because I accidentally deleted that in PhpStorm.I will now go and look if I did that workaround for the local action thing unnecessarily, as well...
Leaving at RTBC, since it's basically just removed code, hope that's OK here.
Comment #17
tstoecklerAhem... #3028768: Remove unnecessary translation workaround in local action plugin
Comment #18
bojanz CreditAttribution: bojanz at Centarro commentedRemoved an unused use statement from EntityLocalTaskTest and added a one-line note to the README about the new feature.
Thank you!
Followup: #3041435: Provide an automatic local task for the Duplicate operation.