Problem/Motivation
If you have a D8/9 site with multilanguage, the "Image linked to content" for the Media switcher setting on the formatter, doesn't work as expected: instead of returning the translated link, it always returns the link to the source node language.
Steps to reproduce
- Create a D8/9 website with two languages.
- Create an article with a media image
- Create a view page with the media image using blazy formatter
- Select "Media switcher: image linked to content"
- Now create a node (with a media image) and translate it
- Go to the view page. On both language the link will refer to the same node (the original node)
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | 3214002-i18n-8.patch | 7.08 KB | gausarts |
| #3 | 3214002-fix_i18n_support_on_links.patch | 1.02 KB | finex |
Issue fork blazy-3214002
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
Comment #2
gausarts commentedThank you.
I don't have a multilingual setup currently, but let's track it down for anyone who are feeling invited to provided patches.
Is it Blazy Filter as I cannot find any of its usages in Blazy Filter, and won't makes sense in Filter either:
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.2/src/Plugin/Filte...
> - Create a view page with the media image using blazy formatter
The usage is here:
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.2/src/BlazyManager...
Since
content_urlcomes from anywhere, we cannot do correction in the above lines.A few candidates:
, appears similar to yours:https://git.drupalcode.org/project/blazy/-/blob/8.x-2.x/src/Dejavu/Blazy...
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.x/src/BlazyFormatt... which leads here: https://git.drupalcode.org/project/blazy/-/blob/8.x-2.x/src/BlazyFormatt...
If it were a field within entities, such as Link as seen at any blazy-related Media/ Paragraphs, etc. formatters, we do have solution here:
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.2/src/BlazyEntity....
Unfortunately your use case appears to fall into Views with image URL as identified above.And indeed, I haven't been able to make it more language aware without proper setup.
Patches are welcome. Thanks.
UPDATED:
Deleted irrelevant lines to your use case since you said blazy formatter, not image formatter.
But because your use case is not Media/ Paragraphs entity formatters, alike, hence node, so the second applied.
Comment #3
finex commentedHi, thank you for the hints. I've updated the BlazyFormatter.php loading the correct language address:
Now it seems working :-)
Comment #4
gausarts commentedThank you.
Just minor nits:
We should avoid calling non-di like
\Drupal::at non-static classes.But I need your help to verify if still working or not, basically replacing
\Drupal::languageManager()->getCurrentLanguage()->getId();with$items->getLangcode();:https://git.drupalcode.org/project/drupal/-/blob/9.0.13/core/lib/Drupal/...
https://git.drupalcode.org/project/drupal/-/blob/8.9.15/core/lib/Drupal/...
Then the previous mentioned sample might apply with minor adjustments:
https://git.drupalcode.org/project/blazy/-/blob/8.x-2.x/src/BlazyEntity....
Perhaps something like:
Other minor nits are no big-deal:
https://www.drupal.org/pift-ci-job/2065050
Please focus on verifying whether the above replacement work or not instead.
Feel free to update your patch if working. If not, we might need to figure out another alternative.
Thanks again.
Comment #5
finex commentedHi, your solution doesn't work because
$items->getLangcode();always returns the default language even if the media entity type is translatable (and the translation exists).Comment #6
gausarts commentedBad suggestions, then :)
The last, ugly, resort is moving that
\Drupal::languageManager()into static method such as here:https://git.drupalcode.org/project/blazy/-/blob/8.x-2.x/src/Blazy.php#L489
Please update your patch by adding this method into
Blazy.php:Then replace
\Drupal::languageManager()in your patch byBlazy::languageManager()This should do till we have proper DI, perhaps at Blazy 3+.
Comment #7
gausarts commentedWe need to have a more permanent solution as it appears we have other similar warnings, not errors, of course:
#6 is just temporary.
Perhaps adding it in as service tags will be less temporary, although still temporary till the next branch works.
Comment #8
gausarts commentedAttached to add the language manager service and also to address the other similar issue once.
Comment #10
gausarts commentedFixed minor CS.
Be sure to run the update, or clear cache in the least as we are adding a service.
Committed. Thank you for contribution.
Comment #11
anybodySadly, I just ran into an issue with this fix:
This part isn't enough as it seems. The site is multilingual but that doesn't mean the selected translation exists:
We further need a
check:
Tested and works! I'll create a merge request.
Comment #13
anybodyMR created. Would ne nice to have a new stable release containing this fix.
Comment #14
gausarts commentedToo late to the party but ok 😂
Can we remove
$this->languageManager->isMultilingual() &&as said in #4?Comment #15
anybodyWell yes I guess we could. On the other hand the $this->languageManager->isMultilingual() is very fast, so the combination is smart as only the first condition is evaluated on non-multilang projects. You decide :)
Logically the first condition is not needed.
Comment #16
gausarts commentedYes, please. Nobody complained at two other places with just that so far.
Comment #17
anybodyUpdated!
Comment #19
gausarts commentedI had delayed 2 weeks for 2.3 from the initial plan to accommodate these kinds of misses.
I released 2.3 before this weekend to test the waters, normally full releases got attentions better than DEV.
We'll release another hot fix by weekend before weekend warriors attack. And also to re-check other potential misses, if any.
Committed. Thank you for contribution.
Comment #20
volker23 commentedThanks @Anybody, switching from 2.3.0 to the current dev fixes the error i was encountering after the update to 2.3.0
Comment #21
finex commentedThanks for the fix!
Comment #22
kreatil commentedEncountered the same error as #20. Switching to current dev fixed it for me.