Entity::toUrl() currently uses a hardcoded special case to fill the revision_id parameter only on one route ("revision").
if ($rel === 'revision' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
}
However, entity annotations can provide other revision-specific routes, such as "revision_revert" or "revision_delete". Maybe we could expand this and add it to all routes that match /^revision\b.*/ rather than only /^revision$/.
(Ideally, it should be set on all routes that accept a {$type}_revision parameter, but the actual parameters accepted by a route don't seem to be available to the code as it is, and if we set it on routes that don't have one, we'll likely break something. The expanded revision-* or revision_* match would be easier to implement.)
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | drupal_core-8.9.7-2927077-33.patch | 2.53 KB | nono95230 |
| #25 | 2927077-interdiff-21-25.txt | 1.2 KB | cburschka |
| #25 | 2927077-25.patch | 2.54 KB | cburschka |
| #21 | 2927077-21-testonly.patch | 2.03 KB | cburschka |
| #21 | 2927077-21.patch | 2.5 KB | cburschka |
Comments
Comment #2
cburschka.
Comment #3
cburschka.
Comment #4
cburschkaThis expands the logic from
revisionto all relationship types that start with revision.I'm still not so happy about attaching special behavior to link template names, tbh. It's already undocumented magic as it is, and this patch adds even more magic.
The original code was added in #2456599: Field node_field_revision.title needs to use an entity-aware formatter in Views, maybe we could revisit the problem and find a more robust way?
Comment #5
cburschkaActual patch attached.
Comment #6
sam152 commentedI wonder if there is a reason the param isn't just added for all link templates? If the param doesn't exist in the URL, it would just be ignored right?
Comment #7
cburschkaI don't think it would lead to outright errors when the Url is rendered, but there are places in core which make assumptions about the parameters in the unrendered object.
Just as an example, menu_link_content.module does this:
This wouldn't work correctly anymore, as
loadLinksByRoute('entity.node.canonical', ['node' => 1, 'node_revision' => 1])would not pick up the menu link for/node/1.I suspect the only robust workaround would be to actually render and then parse the Url object, which seems like a big overhead.
Comment #9
jibranComment #11
jibranReroll for 8.7.x
Comment #12
hchonovRe #6:
I think this would be the best option.
Re #7
Unfortunately this is correct.
But what we can do is to return the parameter in
\Drupal\Core\Entity\Entity::urlRouteParameters()and in\Drupal\Core\Entity\Entity::toUrl()pass it to the URL object only in case this parameter is present on the route. What about something like this:Make
\Drupal\Core\Routing\RouteMatch::getParameterNames()somehow accessible -An approach based on the 3rd option:
Now that we know the necessary parameters we can remove the unnecessary ones:
Comment #13
hchonovComment #15
dpiReroll since #1883744: Convert Entity to EntityBase for consistency with ContentEntityBase and ConfigEntityBase
Comment #16
yogeshmpawarSetting back to Needs Review & Triggering bots.
Comment #17
aaronmchaleManually tested #15 while working on #2350939-77: Implement a generic revision UI today, and it works as expxcted, tested using the link template
revision-revert-formon theversion_historyroute, using theblock_contentEntity Type.This is now an additional blocker for the issue I just linked.
Comment #18
tstoecklerTo be honest I think #12 is too complex if we can fix the actual issue with a patch as simple as #5 / #11 / #15. I agree with everyone here that the current API around this is super weird but it is the API we have. And #12 for example wouldn't work with "extra" route parameters which do not belong to variables, such as the entity type ID on the collection route. So I think #15 (or something like it is the way to go).
This is rightly marked "Needs tests", however, so marking "needs work" for that.
Comment #21
cburschkaHere we can extend
EntityUrlTest::testToUrlLinkTemplateRevision()to test a link template that starts with "revision". Interdiff = testonly patch.Comment #23
cburschkaTestonly patch is designed to fail.
Comment #24
hchonovI think this would be more understandable if we move
$link_templateout of the method call and reassign it:$link_template = $link_template === 'canonical' ? 'revision' : $link_template;Feel free to set to RTBC when done :).
Comment #25
cburschkaThanks! :)
Yeah, that didn't look right to me either.
Comment #26
aaronmchalePerhaps adding an inline comment here would be beneficial so that it's easy to see what the impact of this is. I'm also sort of split on whether strpos is fine or whether substr would be more readable here.
Comment #27
hchonovI think that the code is self explanatory and does not need any additional documentation. Even if we add it I do not see the added value of documenting a simple condition.
Comment #28
larowlanAdding review credit for @AaronMcHale for manual testing and @hchonov for subsystem-maintainer review
Comment #30
larowlanCommitted 8350f68 and pushed to 9.1.x. Thanks!
Updating some items this blocks
Comment #31
aaronmchaleThanks @larowlan, great to see this small yet important patch land, well done everyone!
Comment #33
nono95230 commentedI add to this issue the same patch, compatible with version 8.9.7 of the Drupal core.
Comment #34
kim.pepper