Get vars removed from oembed media url

Created on 6 February 2024, 7 months ago
Updated 12 February 2024, 7 months ago

Problem/Motivation

Media > Remote video's video url gets stripped of autoplay and mute variables.
This video url: https://youtu.be/[video-id]&autoplay=1&mute=1
.. gets turned into this: https://www.youtube.com/embed/[video-id]?feature=oembed
.. so &autoplay=1&mute=1 variables get stripped.

Steps to reproduce

1. Create new media: remote video and add a youtube url with &autoplay=1&mute=1 variables.
2. When you print that media out, then it gets turned into an iframe inside an iframe.
The first iframe has an url of https://[domain]/media/oembed?url=[encoded youtube url that includes the variables]&max_width=0&max_height=0&hash=...
The second iframe gets an url of https://www.youtube.com/embed/[video-id]?feature=oembed <- no &autoplay=1&mute=1 variables.

Proposed resolution

Don't strip url get variables for media: remote video.

✨ Feature request
Status

Closed: works as designed

Version

10.2 ✨

Component
MediaΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡ͺπŸ‡ͺEstonia hannes.himma

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

Comments & Activities

  • Issue created by @hannes.himma
  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    This may be more of a feature request than a bug.

  • πŸ‡ͺπŸ‡ͺEstonia hannes.himma

    What do you mean by that cilefen? ("This may be more of a feature request than a bug.")

    I am not asking for new features - I am asking not to break the remote video's url when handling it.

    There is a lot of functionality that gets broken because of how Drupal's media module handles video urls.

    For example youtube has GET variables you can add to the end of the video url when embedding a video, that control various aspects from autoplay and mute to the related videos that get shown. The current way of handling media remote video urls (1. take youtube video ID 2. remove everything else) breaks all of that functionality.

    Again, I am not asking for new functionality, I am just asking not to completely annihilate the url when handling it.

  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    Does YouTube support those parameters with oembed?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Autoplay and mute can be configured from the formatter in the manage display screen

  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    So, this is "works as designed"?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Hmm looks like that formatter is provided by a module other than core.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Yes I'm thinking of video_embed_field.

    It should be possible to add a custom formatter for Oembed in core though.

  • πŸ‡ͺπŸ‡ͺEstonia hannes.himma

    Does YouTube support those parameters with oembed?

    Yes, of course YouTube supports those parameters with oembed :)

    I've made a temporary js fix that solves the problem, but isn't the "right" way of doing things:

    function fixAutoplay() {
      jQuery('iframe[src^="/et/media/oembed?"]').each(function () {
        if (jQuery(this).attr('src').indexOf('autoplay%3D1') == -1)
          return;
    
        var nestedIframes = jQuery(this).contents().find('iframe');
        if (nestedIframes.length > 0) {
          nestedIframes.each(function () {
            var src = jQuery(this).attr('src');
            if (src) {
              src += (src.indexOf('?') === -1 ? '?' : '&') + 'autoplay=1&mute=1';
              jQuery(this).attr('src', src);
            }
          });
        }
      });
    }

    The right way of doing things would be to just modify the code that strips the url of all other variables besides the video ID, would it not? :)

  • πŸ‡ͺπŸ‡ͺEstonia hannes.himma

    Larowlan, why did you change the category to feature request instead of a bug?

    The infromation user outputted (youtube embed url) gets removed by accident that shouldn't be removed. This is a bug, plain and simple. I do not want to use video_embed_field - I am talking about using Media.

    video_embed_field even has a disclaimer:

    If you are installing this module for integration with a media library, core already contains all the tools required for embedding remotely hosted videos. This module should no longer be required for most use cases and should be avoided if possible.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Looking at the code path in \Drupal\media\OEmbed\UrlResolver::getResourceUrl

    This ends up in \Drupal\media\OEmbed\Endpoint::buildResourceUrl

    This ends up calling the endpoint with the Url passed and the oembed resource returns the markup for the iframe.

    For example for youtube, it ends up at a URL like so https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=IBAFP...

    If you visit that URL you can see in the response that Youtube returns the stripped version.

    In fact, Drupal still stores the originally submitted url

    \Drupal\media\Entity\Media::load(5026)->toArray()['field_media_oembed_video']
    = [
        [
          "value" => "https://www.youtube.com/watch?v=IBAFPFo2F0U&autoplay=1",
        ],
      ]
    
    

    You can also see from \Drupal\media\Controller\OEmbedIframeController::render that we simply return the html from the oembed provider. So that's what Youtube is giving us in the URL above.

    So in summary, this issue is caused by how YouTube implements Oembed.

    If you wish to do things differently, you can use a different formatter. You have access to the original URL still.

    This is either a feature request to add a formatter that ignores what the oembed provider provides for 'html' or works as designed.

    My opinion is that its the later, we can't special case a formatter for Youtube in a generic oembed field. People can write custom code to create a formatter or contrib project for that if they wish.

  • Status changed to Closed: works as designed 7 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Oh I missed your comment in #10, you found the same thing as me - but I still don't think we should be doing anything with the HTML from the oembed response in core. Especially not special-casing Youtube. That belongs in contrib or custom code.

    On that basis, marking this one.

  • πŸ‡¦πŸ‡²Armenia le72 Yerevan πŸ‡¦πŸ‡²

    Thank you for the post.
    I am facing the same issue. So, what is the best approach to have muted (or looped) YouTube media videos embedded?
    Maybe a module with a field formatter supporting YouTube and Vimeo videos?

Production build 0.71.5 2024