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

  1. Setup a site with multiple languages.
  2. Add a translatable media type which allows translation of the media source, e.g., image.
  3. 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.
  4. Create a node in the default translation and add some media.
  5. Translate the node and edit the media through the modal. Here, remove the media from the default translation and upload a new media.
  6. 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.

Command icon 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

simonbaese created an issue. See original summary.

simonbaese’s picture

Status: Active » Needs review
simonbaese’s picture

After 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 MediaForm as follows:

 public function buildForm(array $form, FormStateInterface $form_state) {
    if ($this->getRequest()->query->get('media_library_edit') === 'ajax') {
      $form_state->set('langcode', $this->languageManager->getCurrentLanguage()->getId());
    }
    return parent::buildForm($form, $form_state);
  }

When providing a langcode the ContentEntityForm steers the behavior to add a new translation in the init() method. Unfortunately, there is no hook early enough to inject this behavior. Extending the MediaForm class is probably too intrusive. Also, there are more issues arising, i.e.:

  • Modal title is not displayed correctly, when creating translation.
  • Default values from default translation of media entity are not populated.

Hence, we may would need to stick to the suggested approach in the patch.

omarlopesino made their first commit to this issue’s fork.

omarlopesino’s picture

The 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.

simonbaese’s picture

@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.

nagy.balint’s picture

StatusFileSize
new1.86 KB

MR16 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.

nagy.balint’s picture

StatusFileSize
new2.1 KB

So 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

nagy.balint’s picture

So the change is at

$media_original->addTranslation($current_langcode, $media->toArray());
$media = $media_original;
simonbaese’s picture

I 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 the und langcode a little better?

nagy.balint’s picture

The error message is similar to the related issue: #3066113: How to translate an entity programmatically

What I got is

Drupal\Core\Entity\EntityStorageException: Invalid translation language (und) specified. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 818 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

#0 core/modules/media/src/MediaStorage.php(29): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\media\Entity\Media))
#1 core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\media\MediaStorage->save(Object(Drupal\media\Entity\Media))
#2 modules/contrib/media_library_edit/media_library_edit.module(204): Drupal\Core\Entity\EntityBase->save()

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.

nagy.balint’s picture

I 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.

simonbaese changed the visibility of the branch 3404209-missing-active-language-2 to hidden.

simonbaese’s picture

Applied suggestions by @nagy.balint and updated branch.

proteo’s picture

Status: Needs review » Reviewed & tested by the community

Spent 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.

rakugaki made their first commit to this issue’s fork.

ahebrank made their first commit to this issue’s fork.

  • ahebrank committed f82f849f on 3.0.x authored by simonbaese
    Issue #3404209: Missing active language when adding translation to media...
ahebrank’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.