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.
Follow-up to issue https://www.drupal.org/node/1470018#comment-9338279
When adding an image within the wysiwyg, fields values of the entity are shown in default languages. They need to show the translated values if available.
Comments
Comment #1
pmusaraj CreditAttribution: pmusaraj commentedFixing this properly would require passing the language of the current node to the media browser. Without knowing the language of the current node, there is no way to load the correct file entity language.
There is a workaround, though. We can use two hooks to load the correct alt and title fields for the file entity when displaying the image. This would bypass the form in the popup, therefore, a second hook allows us to disable those two fields in the popup window.
Code below assume it's in a module called testmodule:
Comment #2
David_Rothstein CreditAttribution: David_Rothstein commentedHere's a patch that's a partial fix (I thought of creating a separate issue for this, but seems to make more sense in this one).
It makes things work correctly on display, although it's pretty hacky.
It does not solve the problem of being able to add overridden title/alt text for a particular language when embedding an image within the media browser; I believe if you type something in there it will still always be used as the English value. But it does handle the case where the image file entity has translated alt/title fields attached to it, and it makes sure that the correct language is used for those when the embedded image is being rendered. There are two fixes:
I've only tested this patch against an older version of the Media module (alpha3) and am attaching a version of the patch for that too, but the version for 7.x-2.x was a pretty straightforward reroll of that.
Comment #3
David_Rothstein CreditAttribution: David_Rothstein at Tag1 Consulting commentedNew patches fixing a logic error - the previous version compared the 'alt' and 'title' overrides to the alt/title text stored on the entity itself, but instead should be comparing to the field overrides that were embedded in the WYSIWYG (which is basically what the documentation said it was already doing).
Without this fix, the patch essentially stopped working if someone went back and edited the original English version of the file to change the alt/title text there to something else.
Comment #4
mgiffordWould be great if this were included @David_Rothstein's patch still works.
Comment #5
chx CreditAttribution: chx commented@David_Rothstein's patch is great but with the latest dev it does not seem to be enough any more as the return value from
media_wysiwyg_filter_field_parser
is set completely on the field instead of merging in. Ie the English version was entered in the WYSIWYG editor and then later a German version was added so after file load you have$file->some_alt['en']
$file->some_alt['de']
but the override only contains$file->some_alt['en']
and the current code sets that to$file->some_alt
destroying the German translation.Reviewing
media_wysiwyg_filter_field_parser
found that the merging is basically happening there already so I passed in$file
then rewrote the merging. I also found traces of this wanting to happen asmedia_wysiwyg_format_form
already passed in$file
to this function.Comment #6
sylus CreditAttribution: sylus commentedJust for my own edification is this related to this issue? I am thinking not but wanted to make sure not duplicating any logic with langcode handling. Thx!
#2129273: Pass langcode to media_wysiwyg_token_to_markup resolving media alt attributes in interface
Comment #7
peximo CreditAttribution: peximo commentedRerolled. Testing the old patch I found that overrides works fine with source language but it seems to me there are some problems with translated content.
Rerolled patch doesn’t fix this problem, it just updates the changes to the last version.
Comment #10
joseph.olstad@peximo, I'm kind of surprised to see this issue as I haven't noticed it. I'm not sure any patch is needed, I'll have to have a closer look myself. Meanwhile I've got some steps for correctly enabling file_entities (media) as entity_translate enabled:
See these instructions.
#2838315: Support for translating file entities
Have you configured file_entity to be entity translatable as per these instructions?
Comment #11
joseph.olstadin addition:
7.x-4.x is really good from what I can tell, I haven't noticed any problems with it yet.
Kaare from the University of Oslo developed the 7.x-4.x version and he did an excellent job. You might want to try it out and report your results.
Please review my previous comment with configuration instructions for file_entity and entity_translation enabling/configuring of file entities.
Comment #12
peximo CreditAttribution: peximo commented@joseph.olstad, thanks for your reply. I've a site with Media 2.x and unfortunately I can't upgrade this module to 4.x.
I've Media 7.x-2.0-beta1 with this patch applied and all works well, I've read the provided instructions and everything seems to me correctly configured.
After the update to 7.x-2.20 the translated alt and title are showed in a wrong language (the source language).
With this rerolled patch everything works good again.
Comment #13
joseph.olstadthe last patch applies correctly (with fuzz) on the current HEAD of the media 7.x-2.x branch
Something must be up with the testbot, I retriggered just now.
Comment #14
joseph.olstadsee if this reroll will run
Comment #15
joseph.olstadtest runner didn't like the fuzz, the new patch has no fuzz.
Comment #16
joseph.olstadComment #17
joseph.olstadComment #20
joseph.olstadPatch needs to use array() syntax, not []
array() is not deprecated yet so it's still good.**EDIT** looks like the issue was just a glitch with the testbot
Comment #21
joseph.olstadComment #22
joseph.olstadneeds a port to 4x, it's conflicts everywhere.
reroll for 3x, same patch, not sure why the testbot is being so picky about fuzz
Comment #23
joseph.olstadComment #24
steinmb CreditAttribution: steinmb as a volunteer commented