Missing active language when adding translation to media while editing through modal (AJAX)

Created on 26 November 2023, 12 months ago
Updated 21 June 2024, 5 months ago

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 Add content_translation support Needs work . 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.

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇩🇪Germany simonbaese Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @simonbaese
  • Status changed to Needs review 12 months ago
  • 🇩🇪Germany simonbaese Berlin

    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);
      }
    

    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.

  • First commit to issue fork.
  • Merge request !18Issue #3404209: Get media from context → (Open) created by omarlopesino
  • 🇪🇸Spain omarlopesino

    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.

  • 🇩🇪Germany simonbaese Berlin

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

  • 🇭🇺Hungary nagy.balint

    MR16 did not work for me cause it kept throwing Invalid translation language (und) specified.

    I made a simpler patch, as actually when adding translation we normally don't need to save the return value of the addTranslation method, but we can simply save the entity.

    And this works for me at least.

  • 🇭🇺Hungary nagy.balint

    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.

  • 🇭🇺Hungary nagy.balint

    So the change is at

    $media_original->addTranslation($current_langcode, $media->toArray());
    $media = $media_original;
  • 🇩🇪Germany simonbaese Berlin

    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?

  • 🇭🇺Hungary nagy.balint

    The error message is similar to the related issue: 💬 How to translate an entity programmatically Closed: outdated

    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.

  • 🇭🇺Hungary nagy.balint

    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.

  • 🇩🇪Germany simonbaese Berlin

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

  • 🇩🇪Germany simonbaese Berlin

    Applied suggestions by @nagy.balint and updated branch.

Production build 0.71.5 2024