Mapped media fields are overridden with metadata on translation save

Created on 13 September 2018, over 6 years ago
Updated 19 September 2024, 3 months ago

Problem/Motivation

A media title is overridden with filename after saving of media translation with different image for target language

To reproduce:

  • Install Drupal 8.6.0 in English
  • Enable these modules: Core: Media, Multilingual: Content Translation
  • Add a language ( /admin/config/regional/language )
  • Check the 'Media' custom language settings in /admin/config/regional/content-language and make image media type translatable (translation for Image field should be enabled too).
  • Add an image (/media/add/image) and translate it: upload different image and change title

Actual result: media title is overridden with file name in translation.

I guess there is mistake in Media::prepareSave() method:

        foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
          // Only save value in entity field if empty. Do not overwrite existing
          // data.
          if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
            $translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
          }
        }

Looks like current condtition is not OK, and OR condition should be replaced with AND condition:

if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() && $translation->hasSourceFieldChanged())) {

In this way title will be overridden only in case when it is empty.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs review

Version

11.0 🔥

Component
Media 

Last updated 3 days ago

Created by

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇭Switzerland ayalon

    I have tested patch #62 with Drupal 9.5.3 and it successfully resolves the issue, that you cannot change the name ov a previously saved media entity translation.

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States ndewhurst USA

    The patch in #62 prevents the specific problem outlined in the description, but it introduces a regression. Specifically, metadata will never be mapped to entity fields, even if those fields are empty, unless the source field changes. This does not appear to match the original intent here.
    It might not be a noticeable issue for most people because most metadata won't change unless the media source changes - e.g. if you edit an Image-sourced media entity and replace the image file. However, it's perfectly valid for there to be all sorts of other metadata that might be updated somewhere without the media source changing - e.g. a transcript on an externally-hosted video, a caption for an image stored in an external DAM, etc.
    I wonder if we could restate the problem by defining the expected behavior as:

    Available metadata should always be used to populate mapped fields when media entities are saved/resaved and the target fields are empty. When changing a media source field, we should expect any mapped fields to be updated with metadata-derived values, unless we are simultaneously providing alternative values for some of those fields.

    I know it doesn't avoid the "I previously customized the media name, and I was hoping my custom name would persist after replacing the source file" scenario...but it seems to balance the concerns in this thread with the original intent.
    Here's a patch that tries to put the aforementioned behavior into practice. If someone is able to look at tests for it or otherwise weigh in, that would be cool.

  • 🇺🇸United States ndewhurst USA

    Updating patch to work with new media entity creation, improve empty value handling, and revise comments.

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    29,787 pass, 6 fail
  • 🇮🇳India Ajeet Tiwari

    Please review.

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States ndewhurst USA
    +++ b/core/modules/media/src/Entity/Media.php
    @@ -433,7 +432,7 @@ public function prepareSave() {
    -          if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
    +          if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() && $translation->hasSourceFieldChanged())) {
    

    @Ajeet Tiwari - this proposal is the same as in #2 🐛 Mapped media fields are overridden with metadata on translation save Needs review . I don't believe this is good, because mapped fields that are empty should be eligible for population regardless of whether the source field is changing.

  • 🇭🇺Hungary HAL 9000

    Hi, 62's patch has been re-rolled to work with 10.2.

  • last update 9 months ago
    Patch Failed to Apply
  • 🇩🇪Germany Anybody Porta Westfalica

    I can sadly confirm this issue in all multilanguage projects.

    @ndewhurst you say the patch in #70 is superior. Should this be turned into a MR against 11.x then and also implement tests (eventually the test copied from the existing MR?)

    I agree this is major for all multilingual projects!

  • First commit to issue fork.
  • 🇩🇪Germany Grevil

    Ok, I manually applied patch #70 by @ndewhurst on a new issue branch and created an MR against current 11.x. I'll take a closer look at the patch tomorrow and see if I can get the existing tests to work for the new branch.

  • 🇩🇪Germany Grevil

    Ok something went wrong. I'll get back to it tomorrow.

  • 🇩🇪Germany Anybody Porta Westfalica

    Anybody changed the visibility of the branch 11.x to hidden.

  • 🇩🇪Germany Anybody Porta Westfalica

    Anybody changed the visibility of the branch 9.3.x to hidden.

  • 🇩🇪Germany Anybody Porta Westfalica

    Anybody changed the visibility of the branch 11.x to active.

  • Merge request !8973Applied #70 by @ndewhurst → (Closed) created by Anybody
  • 🇩🇪Germany Anybody Porta Westfalica

    @Grevil: This was indeed pain! I was finally able to apply #70 with MR !8973 (11.x - sorry for the bad naming).
    I think we can now proceed there and hide / close the other MR's?

  • Pipeline finished with Failed
    5 months ago
    Total: 602s
    #237536
  • Merge request !8983Applied #70 by @ndewhurst → (Open) created by Anybody
  • Pipeline finished with Success
    5 months ago
    Total: 420s
    #238073
  • 🇩🇪Germany Grevil

    Grevil changed the visibility of the branch 2999370-mapped-media-fields-11.x to hidden.

  • Pipeline finished with Success
    5 months ago
    Total: 393s
    #238419
  • 🇩🇪Germany Grevil

    Grevil changed the visibility of the branch 2999370-mapped-media-fields to hidden.

  • Pipeline finished with Failed
    5 months ago
    Total: 401s
    #239305
  • Status changed to Needs review 5 months ago
  • 🇩🇪Germany Grevil

    MR! 8983, containing the changes of patch #70 and the test provided with patch #62 seems to work great! The new code is a bit harder to read, but I see no caveats with it on my end.

    Unsure if the provided test is enough, or if we should somehow recreate the case mentioned by @ndewhurst in #69:

    However, it's perfectly valid for there to be all sorts of other metadata that might be updated somewhere without the media source changing - e.g. a transcript on an externally-hosted video, a caption for an image stored in an external DAM, etc.

    Setting this to "Needs Review" for now, so a core maintainer can decide if the provided test is enough.

  • Pipeline finished with Success
    5 months ago
    Total: 1315s
    #239344
  • 🇩🇪Germany Anybody Porta Westfalica

    @Grevil: I tried if the "Test only" functionality in the Pipeline can help here to show the test-only test fails (https://git.drupalcode.org/issue/drupal-2999370/-/pipelines/239344/) but I'm not familiar enough with that. So could you eventually post a test-only branch to show the test fails without the fixes? Or do you know how to do that correctly?

  • Merge request !8999Draft: Test only, do NOT push → (Open) created by Grevil
  • Pipeline finished with Failed
    5 months ago
    Total: 739s
    #239569
  • 🇩🇪Germany Grevil

    See output of 2999370-mapped-media-fields-fix-11.x-test-only, the test fails without the changes as expected! 🎉

  • Status changed to RTBC 5 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Yeah GREAT @Grevil! RTBC for MR !8983!
    Would still be good if others here could also test the MR for edge-cases, so we can get this fixed finally. Thank you!

  • 🇳🇿New Zealand quietone

    I do like the older issue getting fixed. Thanks for getting this to RTBC.

    I've added the standard template to the issue summary, can someone fill out the proposed resolution here.

    I reviewed the MR and left some suggestions and questions there. Setting to needs work for that.

  • Status changed to Needs work 4 months ago
  • Pipeline finished with Failed
    4 months ago
    Total: 1140s
    #254868
  • Pipeline finished with Success
    4 months ago
    Total: 1018s
    #254905
  • Pipeline finished with Success
    4 months ago
    Total: 707s
    #254918
  • Status changed to Needs review 4 months ago
  • 🇩🇪Germany Grevil

    Alright, adjusted the code based on the suggestions by @quietone. Please review once again!

  • Pipeline finished with Success
    4 months ago
    Total: 667s
    #254947
  • Status changed to RTBC 4 months ago
  • 🇺🇸United States smustgrave

    Believe feedback has been addressed on this one.

  • Status changed to Needs work 3 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    I've added some comments on the MR to address.

  • Pipeline finished with Failed
    3 months ago
    Total: 677s
    #280072
  • Pipeline finished with Canceled
    3 months ago
    Total: 91s
    #280248
  • Pipeline finished with Success
    3 months ago
    Total: 681s
    #280251
  • Status changed to Needs review 3 months ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Added another suggestion to the MR.

  • Status changed to Needs work 3 months ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • Pipeline finished with Failed
    3 months ago
    Total: 451s
    #287342
  • Status changed to Needs review 3 months ago
  • 🇺🇸United States smustgrave

    There are 2 MRs could one be hidden please.

    Assuming the one to review is 8983 but that one appears to have test failures, moving to NW for investigation.

  • 🇩🇪Germany Grevil

    grevil changed the visibility of the branch 2999370-mapped-media-fields-fix-11.x-test-only to hidden.

  • Pipeline finished with Success
    3 months ago
    Total: 1026s
    #292132
  • 🇩🇪Germany Grevil

    Simple rebase fixed it again. Back to needs review!

  • 🇺🇸United States smustgrave

    My mistake should of noticed before but issue summary is incomplete.

Production build 0.71.5 2024