I tracked down a very hard-to-find bug in the language switcher block to an issue with how ConfigurableLanguageManager::getLanguageSwitchLinks gets its links. It iterates through the negotiation methods provided by LanguageNegotiator::getNegotiationMethods and uses the first one that supports language switch links. However, these methods are not ordered by weight - in my case, they are returned in the order language-session, language-url, even though the second one has a lower weight.
If the site is set to use language prefixes (/en/, /de/, /fr/, etc.) then this results in links like /en/node/123?language=de, which will not actually change the language as the prefix takes precedence.
Adding a weight-sort in LanguageNegotiator::getNegotiationMethods (immediately after the array_intersect) fixes this, and lets the LanguageUrl negotiator handle the switcher links, returning the correct /de/node/123 or finding language-specific aliases when available.
Comment | File | Size | Author |
---|---|---|---|
#17 | interdiff-16-17.txt | 1.29 KB | Krzysztof Domański |
#17 | 2843822-17.patch | 2.02 KB | Krzysztof Domański |
#16 | 2843822.patch | 2.04 KB | borisson_ |
Comments
Comment #2
cburschkaComment #3
cburschkaHere is a patch and an accompanying kernel test.
(The test may need to be expanded slightly. It will definitely pass with the patch, but it might not reliably fail without the fix.)
Comment #7
borisson_The patch still applies. I ran the test without the patch in
LanguageNegotiator
and it was still green, so the test doesn't test what it think it's testing.Comment #9
borisson_I changed the code and test to make sure that this now tests what it's supposed to.
Comment #11
larowlanCould this perhaps given a more generic name such as LanguageNegotiatorTest to accommodate future tests for that class.
We could then add a @coversDefaultClass annotation too.
Other than that, this looks good to me
Comment #12
borisson_Fixed those remarks.
Comment #13
borisson_Also fixed the CS error.
Comment #14
joachim CreditAttribution: joachim as a volunteer commentedShould the documentation for this be changed to say that they are sorted?
Use
so the class can be imported.
Comment #15
borisson_I don't think we should update the documentation (#14.1). This would mean we'd have to either update the interface, and we're not sure that all implementations do the sort or copy the docs to this class.
The other change is valid, we should do that.
Comment #16
borisson_Comment #17
Krzysztof DomańskiSmall fix.
Comment #19
idebr CreditAttribution: idebr at iO commentedClosed #2906210: Languages detection order are not preserved (Language doesn't switch) as a duplicate issue.
Comment #26
quietone CreditAttribution: quietone at PreviousNext commentedI tested this on Drupal 10.0.x, demo_umami install and was not able to reproduce this error. I followed the steps given in the Issue Summary.
Therefore, closing as outdated.
If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").
Adding related issue, #3007386: Language negotiator weights should be changeable in settings.php which has a similar solution.
Thanks!