Provide missing title attribute for Media Oembed

Created on 29 April 2021, over 4 years ago
Updated 3 April 2024, over 1 year ago

Problem/Motivation

When sites are using the core Media library to embed videos via Oembed, a YouTube video added with the core Media module and rendered via oEmbed will produce an outer iframe that includes a title attribute, but renders an inner iframe that does not include the title attribute.

This is described more fully at https://www.drupal.org/project/drupal/issues/3085545 πŸ› title attribute still not added to oEmbed iframe Postponed

While the name of module, iframe_title_filter, indicates its original focus of solely providing a text format filter to provide the missing title attribute for any iframe inserted into a WYSIWYG, if the goal of the module is to fix missing title attributes on iframes when possible (rather than ONLY in WYSIWYGs), we should consider fixing this via this module, at least until Drupal core provides its own fix.

Steps to reproduce

1. Enable media and media_library
2. Add a field of type "Media" to a node type, set to allow media of type remote_video.
3. Add a node populating the media field with a YouTube video (https://www.youtube.com/watch?v=iyjtK8jPdjM)
4. View the node. The wrapping iframe contains a title attribute, but the interior iframe does not.

Proposed resolution

function iframe_title_filter_preprocess_media_oembed_iframe(array &$variables) {
  /** @var \Drupal\media\OEmbed\Resource $resource */
  $resource = $variables['resource'];
  $media = $variables['media'];
  $html_dom = Html::load($media);
  $iframes = $html_dom->getElementsByTagName('iframe');
  foreach ($iframes as $iframe) {
    if (!$iframe->hasAttribute('title')) {
      $title = $resource->getTitle() ?? "Embedded content from " . $resource->getProvider()->getName();
      $iframe->setAttribute('title', $title);
    }
  }
  $variables['media'] = Html::serialize($html_dom);
}
✨ Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

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.

Production build 0.71.5 2024