Change record status: 
Project: 
Introduced in branch: 
9.1.x
Introduced in version: 
9.1.0
Description: 

When rendering media that comes from an oEmbed source (e.g. YouTube videos), the media is displayed inside a sparse iframe for security reasons.

The contents of the iframe are generated via a template, media-oembed-iframe.html.twig, and a corresponding theme hook (media_oembed_iframe). In Drupal 9.1 and up, this theme hook now receives a reference to the oEmbed resource value object, which can be useful for preprocess functions or templates which want to make resource-specific changes in the oEmbed iframe.

For example:

function mytheme_preprocess_media_oembed_iframe(array &$variables) {
  /** @var \Drupal\media\OEmbed\Resource $resource */
  $resource = $variables['resource'];
  if ($resource->getProvider()->getName() === 'YouTube') {
    // We are rendering a YouTube video, so modify the URL of the video so that it only shows related videos from the same channel.
    // The video's markup is only available as a string, so we need to use str_replace() to modify the URL.
    $variables['media'] = str_replace('?feature=oembed', '?feature=oembed&rel=0', (string) $variables['media']);
  }
}
Impacts: 
Module developers
Themers