File Share overrides the name of media entity

Created on 30 August 2022, almost 3 years ago
Updated 16 February 2023, over 2 years ago

Problem/Motivation

We want to share media entities, which are PDF files. The name of the files are sanitized (all lower case letters), but the name of the media we want to display is different and more human-readable. If we sync a media entity, two items are synced, a file and a media. The media name, which we want to display, is overwritten by the sanitized file name. This happens in line 134 in PhysicalFile.php.

Steps to reproduce

Create a channel:
langcode: en
status: true
dependencies: {  }
id: media_file
label: 'Media File'
channel_entity_type: media
channel_bundle: file
channel_langcode: und
channel_filters: null
channel_groups: null
channel_sorts: {  }
channel_searches: null
channel_maxsize: 50
access_by_permission: false
authorized_roles: {  }
authorized_users:
  3c0c063f-b541-4bd9-b3b9-71de27e64335: 3c0c063f-b541-4bd9-b3b9-71de27e64335

1. Just create a simple file channel like the one above
2. Go to the target system
3. Just sync one media entity

Two items are synced, and the media name gets overwritten by the file destination.

Proposed resolution

Do not set the file name in the PhysicalFile.php. A better solution is welcome. I am not sure, if this is the best proposal, since I am not 100% familiar with that module.

🐛 Bug report
Status

Closed: works as designed

Version

3.0

Component

Code

Created by

🇩🇪Germany danielspeicher Steisslingen

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

Comments & Activities

Not all content is available!

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

  • 🇫🇷France Grimreaper France 🇫🇷
  • Issue was unassigned.
  • Status changed to Closed: works as designed over 2 years ago
  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    I reproduce the "bug", but this is not related to Entity Share and the mentioned line.

    In PhysicalFile.php:

          $processed_entity->setFilename($this->fileSystem->basename($file_destination));
    

    $processed_entity is the file entity, not the media entity.

    This is due to core/modules/media/src/Entity/Media.php::prepareSave():

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

    So this behavior happens the first time the media is imported on the client website or if the media source has changed. When reimporting, the correct name of the media is correctly present.

    Maybe you can test the module https://www.drupal.org/project/media_name which description let me think this is exactly the use case of the module.

    Or see 🐛 Mapped media fields are overridden with metadata on translation save Needs review .

Production build 0.71.5 2024