You are here

protected function MediaEmbed::applyPerEmbedMediaOverrides in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::applyPerEmbedMediaOverrides()
  2. 10 core/modules/media/src/Plugin/Filter/MediaEmbed.php \Drupal\media\Plugin\Filter\MediaEmbed::applyPerEmbedMediaOverrides()

Applies attribute-based per-media embed overrides of media information.

Currently, this only supports overriding an image media source's `alt` and `title`. Support for more overrides may be added in the future.

Parameters

\DOMElement $node: The HTML tag whose attributes may contain overrides, and if such attributes are applied, they will be considered consumed and will therefore be removed from the HTML.

\Drupal\media\MediaInterface $media: The media entity to apply attribute-based overrides to, if any.

See also

\Drupal\media\Plugin\media\Source\Image

1 call to MediaEmbed::applyPerEmbedMediaOverrides()
MediaEmbed::process in core/modules/media/src/Plugin/Filter/MediaEmbed.php
Performs the filter processing.

File

core/modules/media/src/Plugin/Filter/MediaEmbed.php, line 454

Class

MediaEmbed
Provides a filter to embed media items using a custom tag.

Namespace

Drupal\media\Plugin\Filter

Code

protected function applyPerEmbedMediaOverrides(\DOMElement $node, MediaInterface $media) {
  if ($image_field = $this
    ->getMediaImageSourceField($media)) {
    $settings = $media->{$image_field}
      ->getItemDefinition()
      ->getSettings();
    if (!empty($settings['alt_field']) && $node
      ->hasAttribute('alt')) {

      // Allow the display of the image without an alt tag in special cases.
      // Since setting the value in the EditorMediaDialog to an empty string
      // restores the default value, this allows special cases where the alt
      // text should not be set to the default value, but should be
      // explicitly empty instead so it can be ignored by assistive
      // technologies, such as screen readers.
      if ($node
        ->getAttribute('alt') === '""') {
        $node
          ->setAttribute('alt', NULL);
      }
      $media->{$image_field}->alt = $node
        ->getAttribute('alt');

      // All media entities have a thumbnail. In the case of image media, it
      // is conceivable that a particular view mode chooses to display the
      // thumbnail instead of the image field itself since the thumbnail
      // simply shows a smaller version of the actual media. So we must update
      // its `alt` too. Because its `alt` already is inherited from the image
      // field's `alt` at entity save time.
      // @see \Drupal\media\Plugin\media\Source\Image::getMetadata()
      $media->thumbnail->alt = $node
        ->getAttribute('alt');

      // Delete the consumed attribute.
      $node
        ->removeAttribute('alt');
    }
    if (!empty($settings['title_field']) && $node
      ->hasAttribute('title')) {

      // See above, the explanations for `alt` also apply to `title`.
      $media->{$image_field}->title = $node
        ->getAttribute('title');
      $media->thumbnail->title = $node
        ->getAttribute('title');

      // Delete the consumed attribute.
      $node
        ->removeAttribute('title');
    }
  }
}