content: Add translation for embedded media

Created on 6 April 2025, 19 days ago

Problem/Motivation

Drupal core gives you the possibility to embed media entities via CKEditor. These embedded media are referenced without language information and the matching language version is automatically selected when displaying. But the medium must also have an appropriate translation for this. So far TMGMT does not translate the media entities linked via embed when translating a node.

Proposed resolution

Detect embedded medias and translate them.

Remaining tasks

Write the code and the tests.

User interface changes

Maybe there could an option like options for paragraph fields.

API changes

Should not.

Data model changes

Should not.

Feature request
Status

Active

Version

1.0

Component

Source: Entity

Created by

🇩🇪Germany jan kellermann

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

Comments & Activities

  • Issue created by @jan kellermann
  • 🇩🇪Germany jan kellermann

    I just adapted the core from core to extract the embedded media enities. These can be embedded in every text field, not only the in the node, but also in referenced paragraphs, for example. So we should process every text for referenced medie entities.

    But what to do then?

    The existing program code is based on existing fields. It seems difficult to add further content here; it may be necessary to add pseudo-fields that are added before the translation and processed separately later.

    Another approach could be to create separate jobs on the fly for the connected media.

    I welcome suggestions from maintainers and can then try to provide more code.

    Here is the pure function for extracting:

    
    use Drupal\Core\Entity\EntityRepositoryInterface;
    
    
      /**
       * Process text for embedded media entities.
       *
       * @param string $text
       *   The string to translate.
       *
       * @return array
       *   Array of embedded media entities.
       */
      public function extractEmbedMedia($text) {
        if (stristr($text, '<drupal-media') === FALSE) {
          return [];
        }
    
        $embedded_media = [];
    
        $dom = Html::load($text);
        $xpath = new \DOMXPath($dom);
    
        foreach ($xpath->query('//drupal-media[@data-entity-type="media" and normalize-space(@data-entity-uuid)!=""]') as $node) {
          /** @var \DOMElement $node */
          $uuid = $node->getAttribute('data-entity-uuid');
          $media = $this->entityRepository->loadEntityByUuid('media', $uuid);
          $embedded_media[] = $media;
        }
    
        return $embedded_media;
      }
    
    

    See core/modules/media/src/Plugin/Filter/MediaEmbed.php

Production build 0.71.5 2024