Problem/Motivation
- Enable Multilingual modules
- Add a language on the site (I added "spannish")
- Enable Custom menu link translations on the settings page "admin/config/regional/content-language" and check the "menu link item" as a translatable
- Add a new menu item on a Menu (exemple: admin/structure/menu/manage/main)
- Translate this newly created menu item
- You should be on the page "/es/admin/structure/menu/item/1/edit/translations/add/en/es"
Here, the breadcrumb displays
Note : if we click on any of those "Add" link, it crashes.
The add link links to "/es/admin/structure/menu/item/2/edit/translations/add" for the first one, and "/es/admin/structure/menu/item/2/edit/translations/add/es" for the second.
Remaining tasks
- Find out why the "Add" are displayed twice
- Fix the crash (Not sure about this one, since the "Add" item should not even be here).
I'm putting this as "major" since it display an error by clicking on a link. Not sure if major or normal.
Comment | File | Size | Author |
---|---|---|---|
#76 | 2542834-76.patch | 1.66 KB | ConnBi |
| |||
#72 | 2542834-72.patch | 11.49 KB | segi |
Comments
Comment #1
HazaComment #2
HazaVery small attached that removes the "forced" NULL values that are passed to ContentTranslationController::Add().
It removes the "add" links that does not have any reason to be on this page.
I think that the crash is linked to a page that should not even exist, due to a faulty generated link.
Not sure if this is the right approach.
Comment #3
HazaComment #4
HazaComment #5
camoa CreditAttribution: camoa at Acquia commentedBasically the Add was an attempt to create the link to the same page where they were shown, but that broke in 2 links (something like add/en/es is the right link and it created add/ and then add/es) The main question here is why are those added and if this may happen in some other place.
Comment #6
swentel CreditAttribution: swentel as a volunteer commentedThis happens for any translation add screen, also for content for instance, so I guess it simply happens everywhere.
Comment #7
tstoecklerTagging and re-categorizing.
Comment #8
tstoecklerClosing #2656172: Fatal errors can be triggered on content translation add routes (+ incorrect breadcrumb on add form) as a duplicate.
Comment #9
tstoecklerComment #10
tstoecklerLike I proposed in the duplicate issue, I think we should also remove the NULL language from the edit and delete route.
And we need tests for this.
Comment #11
tstoecklerComment #12
morenstratComment #13
morenstratAdded a test and whitelisted links in NodeViewController.
Comment #14
tstoecklerYeah I had already found that problem with NodeViewController in #2656172: Fatal errors can be triggered on content translation add routes (+ incorrect breadcrumb on add form), but forgot to mention it here.
The test looks good. What is not being tested is the bogus breadcrumb that is there. So marking "Needs work" for that.
Also maybe we can tackle the edit and delete routes here, as I mentioned in #10, but I'm also fine with doing that in a follow-up.
Comment #16
morenstratAdded a test for the breadcrumb links, removed the NULL language from the edit and delete routes and changed the content translation access check to perform the access checks against the unchanged entities from storage.
Comment #17
morenstratComment #19
morenstratComment #20
swentel CreditAttribution: swentel as a volunteer commentedHmm, we should document this somehow for other entity types out there. There's no other way we can control this more upstream in the code ?
Comment #22
morenstratFixed ContentTranslationManageAccessCheckTest.
Comment #23
tstoecklerRe @swentel #20: Yeah, this is not the nicest code out there. I discussed this with @morenstrat in Person.
I personally found blacklisting the content translation links instead of the whitelisting even more hacky, that's why I suggested this approach.
The current code is certainly broken (i.e. even in HEAD). Its just that this patch exposes the broken links.
In General our "API" regarding entity links is very lacking, i.e. there is no way to distinguish e.g. canonical from content-translation-add.
So yeah, not sure about this, but also not sure what else to do...
Comment #24
tstoecklerLooked at the patch now. I have nothing to complain; this looks good to go to me. Because this touches a couple of tricky parts, though, not setting RTBC yet but waiting for another set of eyes, especially an answer from @swentel
Comment #25
swentel CreditAttribution: swentel as a volunteer commentedI don't have any better idea now either, so I won't hold this off. Feel free to RTBC :)
Comment #26
tstoecklerOK, coolio
Comment #27
catchI know it's been discussed a bit, but would like to figure out if this is really the only way to do it.
What about other links not in the list that might be added? Do we know they should always be removed too?
Why can't content_translation alter its own links out of here? Then we'd need neither a blacklist nor a whitelist.
Comment #28
tstoecklerMmhh.. There's no alter hook that gets called because this directly returns the controller result. What we could do is add a
#pre_render
callback inhook_entity_view()
which then does the unsetting for us. I'm going to try that.Comment #29
DuaelFrThanks all for your work here.
This issues becomes a bit hard to follow. Could you move that links removal in another issue so we can focus on breadcrumbs here and get it fixed quicker?
Comment #30
tstoeckler@DuaelFr: I would, if it were possible. The links that are generated in HEAD are broken already, it's just that it happens to go through without a fatal coincidentally. By fixing the bogus optionality of the arguments we are exposing that bug, by actually making the link generation fail. Thus, both bugs need to be fixed together in order to get the tests to pass.
Comment #31
DuaelFr@tstoeckler Ok so you need to change the issue title and summary to reflect that fact :)
Thank you for your enlighting answer!
Comment #32
tstoecklerTrue! Thanks for noting that.
Comment #33
morenstrat@tstoeckler: are you sure we can fix this in a pre render callback? The exception is thrown when the content translation add url is added to the render array.
Comment #34
morenstratI'm not sure about "allowed" changes in patch or minor releases. Would it be an option to add an alter hook to Entity::uriRelationships() so that the content translation module could remove its links?
Comment #35
tstoeckler@morenstrat: He, you are right, good catch. I should really know better by know than to simply claim that something is possible without trying it out... Because, generally we now have
Url
objects, that only get rendered when we actually hit the Twig template I thought this could actually work, but it doesn't at least not just like that.The problem:
NodeViewController
and the two other places this code was copy-pasted to in core -NodePreviewController
andtaxonomy_page_attachments_alter()
- just use$entity->url()
which converts to a string directly, instead of (properly) using$entity->toUrl()
which returns such aUrl
object. So if we change that, we can then implementhook_entity_build_defaults_alter()
in Content Translation to add a#pre_render
callback to the build that removes the respective links from$build['#attached']['html_head_link']
. (Note that despite what I said above, I did not actually try this last part out, so beware... ;-))...except then we hit another problem: In this particular case the
Url
objects don't just end up being rendered by Twig templates (which would work fine) because that would be just too easy... InsteadHtmlResponseAttachmentsProcessor
converts these into render placeholders but it does not account for thehref
key to be aUrl
object. I think it is in-line with our philosophy of "rendering as late as possible" to makeHtmlResponseAttachmentsProcessor
convertUrl
objects to strings if it encounters them, so after spending some quality time trying out a variety of different places to stick these 3 lines, I came up with the following "patch" for\Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink()
which then should make the code suggested in the previous paragraph actually work:... however, I'm not sure that's actually such a good idea - despite the late rendering (which in itself is a good thing) because
$url->toString()
looses cacheability metadata of the URL generation. InNodeViewController
(and the other places) where this is currently being called (because$entity->url()
which is actually being called is just a shorthand for$entity->toUrl()->toString
) we are operating inside of the render context of the main page controller so the metadata does actually get caught in the end. InHtmlResponseAttachmentsProcessor
, however, we are not in any render context as far as I know. So on top of increasing the complexity of the patch a fair bit, pursuing this direction would actually be a regression of the cacheability metadata, unless I'm missing something.Due to all this assigning to @catch for some feedback on how we want to proceed here. I sincerely hope I am making any sense here; I apologize for being a little bit ramble-y. It's fairly late here and I just spent an amount of time larger than I'd like to admit staring at
HtmlResponseAttachmentsProcessor
...And thanks again @morenstart for setting me straight!
Comment #36
tstoecklerRe #34: I guess it really depends on the semantics of the
uriRelationships()
method whether something like that would be appropriate. Content Translation only adds those link-templates in the first place because they are used for the route generation of the content translation routes. So we want Content Translation to show up in the link templates (at least in those specific cases), and since currently$entity->uriRelationships()
is literallyarray_keys($entity->linkTemplates())
it shows up in there as well. So I don't really know how to fix this "properly", to be honest. An alter hook might be a solution, but I'm not sure of all the consequences of this.There is #2113345: Define a mechanism for custom link relationships which aims to provide more of a proper API for link relations (rather than the sort of "free-for-all dumping ground" we have now) but that has seen no activity in almost a yearand has no actual code... :-/
Maybe @catch or someone else who is more knowledgeable about the whole area of link relations can provide some input here.
Comment #37
tstoecklerBecause I still had this applied locally and played around with the site for some other purpose I just found another tidbit that we would need to adress if we were to pursue #27 and later:
I said that
$entity->url()
is equivalent to$entity->toUrl()->toString()
and did not mention the fact that the former actually does a check for$entity->id()
and simply returns an empty string if there is no ID, because I thought this check is not relevant.It actually is, however, we currently rely on it in order for the link-generating code to not fatal. So we would have to adapt (at least)
NodePreviewController
for that, as well. Although, thinking about it more, I'm not sure why we even generate those links for a preview. Unless I'm missing something this seems incredibly pointless.Comment #39
tstoecklerOpened #2751501: NodePreviewController should not render entity links in the HTML head for the point noted in #37.
Comment #40
tstoecklerThis is an attempt at resolving this issue by avoiding the code mentioned by @catch in #27 and instead hardcoding knowledge about the Content Translation routes in
Entity::uriRouteParameters()
- also not very elegant, but completely backwards compatible as far as I know.Note that in order to provide test coverage for the new code in
Entity::uriRouteParameters()
this would have to be postponed on #2751395: Rewrite EntityUrlTest, but not marking as such yet to get some feedback on the approach first.Comment #41
tstoecklerComment #43
tstoecklerJust shooting in the dark here.
Comment #45
tstoecklerOkay, only ever so slightly less elegant. Let's see if this is green.
Comment #46
tstoecklerNow that #2751395: Rewrite EntityUrlTest is in, this could use some test coverage. Still leaving at Needs review, though, for some input on the general direction.
Comment #47
tstoecklerDecided to remove the complex language finding - it's pointless to call this anyway. Added a comment to that extent as well. Also adds some tests.
Comment #48
tstoecklerPatch naming fail.
Comment #53
dravenkRemove those lines, because node/2113345 was fixed.
Reroll. This patch work for me.Thanks.
Comment #55
dravenkJust reroll.
Comment #62
gnugetHere a rebase of #55 for Drupal 8.9.x.
(This might still needs work but I want to know how many tests it is going to break)
Comment #63
gnugetLets try again.
Comment #64
gnugetComment #65
apadernoComment #66
AjitSRerolled for 9.3.x
Comment #67
AjitSFixed the coding standard issues.
Comment #72
segi CreditAttribution: segi at Cheppers commentedRe-rolled the patch for 9.5.x
Comment #73
andypostissue is fixed
Comment #74
apadernoYes, #2113345: Define a mechanism for custom link relationships has been fixed on January, 2017. That comment is not necessary any more.
Comment #76
ConnBi CreditAttribution: ConnBi commentedCreate a patch for drupal 10.1.x