- 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