Problem/Motivation
In a multi-language setup, when editing a translatable media entity through a modal (AJAX) and adding a new translation, the active language is missing (set to x-default). Therefore, the uploaded media is not added corresponding to the correct translation, but rather overwrites the default translation of the media entity. The expected behavior would be that the translation is added corresponding the current language. This problem does not occur if the translations already exist on the media entity.
Steps to reproduce
- Setup a site with multiple languages.
- Add a translatable media type which allows translation of the media source, e.g., image.
- Add a node type with a field that references the media type and allows editing the media through a modal, i.e., select a proper field widget.
- Create a node in the default translation and add some media.
- Translate the node and edit the media through the modal. Here, remove the media from the default translation and upload a new media.
- Expected: New media is added as new translation to the media entity. Problem: Default translation is overwritten and no new translation is added.
This is also somewhat related to #3316163: Add content_translation support. If the workflow there would tackle this problem, this issue becomes obsolete. Nonetheless, this issue is easier to fix, since there are no workflow changes.
Proposed resolution
See merge request. But, this problem could also be caused somewhere upstream. This should be discussed.
Remaining tasks
Since this module does not have extensive test coverage and is about to be replaced by core functionality, we would propose to refrain from adding tests.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | missing-active-language-3404209-10.patch | 2.1 KB | nagy.balint |
Issue fork media_library_edit-3404209
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 #3
simonbaeseComment #4
simonbaeseAfter discussing with a colleague, we both came to the conclusion that the provided patch does not touch the appropriate place to fix this issue. With some more digging, I found out that it is possible to trigger a translation creation form in a modal with an addition to the
MediaFormas follows:When providing a
langcodetheContentEntityFormsteers the behavior to add a new translation in theinit()method. Unfortunately, there is no hook early enough to inject this behavior. Extending theMediaFormclass is probably too intrusive. Also, there are more issues arising, i.e.:Hence, we may would need to stick to the suggested approach in the patch.
Comment #7
omarlopesinoThe MR 16 works only when the page is in the language is needed to translate, but it is possible to add a translation in another language.
Example: mysite.local/node/23/translation/add/en/fr. In this case, the edit URL will appear in the current language, instead of french it will appear the current language.
Please review MR 18 which takes in account target translatiion route parameter, which should work fine.
Comment #8
simonbaese@omarlopesino I don't see how your patch addresses the original issue. If you found another problem, please do not extend the scope of this issue, but rather open a new one.
Comment #9
nagy.balint commentedMR16 did not work for me cause it kept throwing Invalid translation language (und) specified.
This patch worked for me, but now it overwrites the original entity when saving the translation, so further work is required.
Comment #10
nagy.balint commentedSo this new patch is almost the same as MR16, but instead of making $media to be the return value of addTranslation, It receives the original media entity with the added translation, which works better for me.
reference: #3066113: How to translate an entity programmatically
Comment #11
nagy.balint commentedSo the change is at
Comment #12
simonbaeseI am surprised that makes a difference. You see this pattern
$translation = $entity->addTranslation($langcode, $entity->toArray());quite often, even in Drupal core. Can you describe the issue with theundlangcode a little better?Comment #13
nagy.balint commentedThe error message is similar to the related issue: #3066113: How to translate an entity programmatically
What I got is
I got the same issue in a custom migration recently where I added translations to Media.
I had no time to dig very deep unfortunately. But when I search for addTranslation in core, I see examples for both usage.
But saving the media entity this way worked in my migration and also in this patch.
Comment #14
nagy.balint commentedI have 3 translatable field, and 1 term reference which is not translatable on the media entity.
Maybe it has something to do with the not translatable field in that case.
This is also similar #3348690: "Invalid translation language (und) specified" when creating/updating translations
where they changed the metadata.
Comment #16
simonbaeseApplied suggestions by @nagy.balint and updated branch.
Comment #17
proteo commentedSpent hours pulling my hair until I realized the issue was with this module. The patch in #10 works flawlessly everytime on a site with three translation languages. Using Media Library Edit 3.0.4 and Drupal 10.4.5.
Comment #21
ahebrank commented