Problem/Motivation

Maximum width and maximum height can be set for various media types such as video and photo. These are legitimate oembed request parameters which have a specific function.

The height and width values returned by the provider take into account the maximum size settings and aspect ratio of the resource. For example, if both "max width" and "max height" are set to 500, the "width" and "height" returned might be 500 and 300.

Reference: https://oembed.com/

Duplicate issue with a longer explanation: #3310520: oEmbed videos should use returned height and width values

Steps to reproduce

Current behaviour:

  • When set to zero, the provider determines width and height and returns them in the response
  • When set to non-zero, these are sent to the provider but the response is ignored. Instead, Drupal sets the dimensions to these exact values

Expected behaviour:

  • When set to zero, the provider determines width and height and provides them in the response. Drupal sets the dimensions to the "height" and "width" values returned by the provider.
  • When set to non-zero values, these are sent to the provider. Drupal sets the dimensions to the "height" and "width" values returned by the provider.

Proposed resolution

For height and width check resource width/height FIRST

Remaining tasks

NA

User interface changes

NA

API changes

NA

Data model changes

NA

Release notes snippet

When setting oEmbed width/height to 0, the provider width/height will be used.

Original report

From #2831944-227: Implement media source plugin for remote video via oEmbed:

Some oEmbed providers -- notably YouTube -- will completely ignore the max_width and max_height settings of the oEmbed formatter, which can result in a video overflowing a frame that is far too small for it, and that looks extremely weird. Therefore, given a max_width/height and the width/height of the resource, the formatter should choose whichever value is bigger.

Sounds like a pretty good idea, but it could introduce some weirdness and complexity under certain circumstances. This issue is to figure out if and how we should implement that.

Issue fork drupal-2966656

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

phenaproxima created an issue. See original summary.

phenaproxima’s picture

Title: [PP-1] Negotiate max width/height of oEmbed assets more intelligently » Negotiate max width/height of oEmbed assets more intelligently
Status: Postponed » Active

The main issue has landed, so let's have a look at this.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jds1’s picture

I needed responsive YouTube embeds and they weren't working out of the box. I was able to get them working by unsetting the width/height attributes on the oembed wrapper iframe (the iframe that contains the youtube iframe) and adding some additional CSS to .media-oembed-content.

If this is the right approach I could work on a patch, but I'm definitely not thinking of any situations where the width/height on the parent oembed wrapper iframe could need set width/height attributes. Are there? Should we add a "responsive" checkbox to the field display config that removes width/height when checked? Am I out of scope for this issue/should I create a new one?

Thanks all.

phenaproxima’s picture

Issue tags: +oembed

@if-jds, I think you might want to take a look at #3061134: oEmbed videos are not fully responsive, where responsive videos are being worked on more comprehensively. This issue is a more low-level and is concerned with oEmbed providers which play fast and loose with the oEmbed specification.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Wim Leers credited cilefen.

Wim Leers credited imclean.

wim leers’s picture

Priority: Normal » Major
Related issues: +#3310520: oEmbed videos should use returned height and width values

Closed #3310520: oEmbed videos should use returned height and width values as a duplicate. Asked @imclean there to move his patch to this issue. Already going ahead and crediting them here! And @cilefen too, because thanks to him we discovered it was a duplicate :)

Also bumping to Major because this fits under Interfere with normal site visitors' use of the site (for example, content in the wrong language, or validation errors for regular form submissions), even if there is a workaround. — see https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...

imclean’s picture

Assigned: Unassigned » imclean

The only oEmbed type which doesn't include width and height parameters is Link. We should use the returned values where possible. The YouTube problem mentioned in the IS will need to be reviewed as well to see if anything has changed in 4 years.

imclean’s picture

Status: Active » Needs review
StatusFileSize
new2.91 KB

This will have at least 1 fail. I can't seem to get enough information from running the test locally, it just says "FATAL Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest: test runner returned a non-zero error code (2)".

Status: Needs review » Needs work

The last submitted patch, 18: 2966656-18.patch, failed testing. View results

imclean’s picture

TypeError: PHPUnit\Framework\Assert::assertStringContainsString(): Argument #2 ($haystack) must be of type string, null given, called in /var/www/html/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php on line 211

This is probably due to this patch always using ->getWidth() etc. on Rich, Photo and Video types. This is valid so I'll have to look into it.

1) Drupal\Tests\media\FunctionalJavascript\MediaSourceOEmbedVideoTest::testMediaOEmbedVideoSource
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'200'
+'480'

This works fine locally (CLI). I can't see where the 480 comes from if it's requesting the video from Vimeo for the test.

imclean’s picture

Assigned: imclean » Unassigned

I can't run JS tests yet and I'm having trouble getting a fully functional local test environment. I'm still not sure why run-tests.sh doesn't produce more information. The testbot doesn't really say anything either, probably because the assertions don't have a message.

It might be good to add some extra information to the tests, such as which selector it's using and the value it's trying to compare.

imclean’s picture

Test URL and return value.

https://vimeo.com/api/oembed.json?url=https://vimeo.com/7073899&maxwidth...

{
   "type":"video",
   "version":"1.0",
   "provider_name":"Vimeo",
   "provider_url":"https://vimeo.com/",
   "title":"Drupal Rap Video - Schipulcon09",
   "author_name":"Tendenci - The Open Source AMS",
   "author_url":"https://vimeo.com/schipul",
   "is_plus":"0",
   "account_type":"basic",
   "html":"<iframe src=\"https://player.vimeo.com/video/7073899?h=ee3654bb73&amp;app_id=122963\" width=\"100\" height=\"67\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture\" allowfullscreen title=\"Drupal Rap Video - Schipulcon09\"></iframe>",
   "width":100,
   "height":67,
   "duration":91,
   "description":"Special thanks to Tendenci, formerly Schipul for sponsoring this video with training, equipment and time. The open source way. All creative however was self directed by the individuals - A. Hughes (www.schipul.com/ahughes) featuring QCait (www.schipul.com/qcait) - Hands On Drupal\n\nDrupal is a free software package that allows an individual or a community of users to easily publish, manage and organize a wide variety of content on a website.\n\nNeed a little Drupal help or just want to geek out with us?  Visit our www.schipul.com/drupal for more info - we'd love to connect!\n\nGo here for Drupal Common Terms and Suggested Modules : http://schipul.com/en/helpfiles/v/229",
   "thumbnail_url":"https://i.vimeocdn.com/video/29203255-2fa4c623aa778925f094352206b986b2d7cd0a9ad60a185ebf40050eef5ef4ef-d_100x75",
   "thumbnail_width":100,
   "thumbnail_height":67,
   "thumbnail_url_with_play_button":"https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F29203255-2fa4c623aa778925f094352206b986b2d7cd0a9ad60a185ebf40050eef5ef4ef-d_100x75&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png",
   "upload_date":"2009-10-14 19:27:19",
   "video_id":7073899,
   "uri":"/videos/7073899"
}
imclean’s picture

Twitter returns height and width but they're always null.

YouTube has minimum dimensions of 200 x 200.

imclean’s picture

Status: Needs work » Needs review
StatusFileSize
new5.25 KB
imclean’s picture

StatusFileSize
new5.25 KB

Status: Needs review » Needs work

The last submitted patch, 25: 2966656-25.patch, failed testing. View results

imclean’s picture

Status: Needs work » Needs review
StatusFileSize
new862 bytes
new5.25 KB

I can't remember why I changed it 200, I was probably still working out what was testing what. I also couldn't see where the size of 240 was arrived at.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

imclean’s picture

imclean’s picture

Issue summary: View changes

Updated the IS with what I think is the real problem here.

imclean’s picture

The formatter settings.

Video without the patch. The video is slightly stretched in this case with the whitespace caused by the iFrame being set to 1000 x 1000.

Video after applying the patch. The iFrame is set to the size the returned by Vimeo.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

imclean’s picture

Status: Needs work » Needs review
StatusFileSize
new5.44 KB
imclean’s picture

StatusFileSize
new5.39 KB
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative
StatusFileSize
new2.6 MB

Tried testing this with 1000X1000 and the image appears zoomed in to me with the patch.

after

imclean’s picture

Status: Needs work » Needs review
Related issues: +#3061134: oEmbed videos are not fully responsive

This happens if you set the max_width too high for the region the video is in. I'm not sure what the media module should (or can) do here.

That said, it appears to be a thumbnail issue. I can replicate #35 but when I play the video it fits within the iFrame.

The thumbnail is a fixed width. This was handled in another issue - #3088168: Media thumbnail dimensions are wrong for YouTube videos.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

So this seems to be just Vimeo videos and not affecting YouTube ones. Using a Vimeo video I was able to replicate the same as #31.

Moving to NW for an issue summary to specifically call out what is the proposed solution. Might be worth noting this doesn't affect YouTube, even though the original post mentions this issue was primarily with YouTube videos.

imclean’s picture

I'm not sure what else I can add to this. The patch does what's described in the updated IS. Google has problems, there are a few issues around that. In this case, the thumbnail is the problem.

Plain embed:

https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=dPiLx...

With maxwidth and maxheight set:

https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=dPiLx...

The thumbnail size doesn't change, but that also doesn't fully explain the problem here.

Plain Google embed without Drupal has the thumbnail problem you describe in #35. Review the following at various browser widths:

https://www.youtube.com/embed/dPiLxdBOnTs

The Oembed spec states thumbnail dimensions must respect maxwidth and maxheight parameters, but clearly this isn't happening with YouTube.

(As an aside, Vimeo seems to handle responsive thumbnails well.)

What do you think we could do here to get around the problem with YouTube's thumbnails? I'd be happy to look into it, but I don't really have any suggestions, other than a theme or JS hack.

smustgrave’s picture

No don’t need the thumbnail addressed here but I see the expected behavior in the IS but not what clear what was done to address it.

But if you think that’s not needed can remove the tag and put back into review also

imclean’s picture

Status: Needs work » Needs review
Issue tags: -Needs issue summary update
+++ b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php
@@ -224,8 +224,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
-            'width' => $max_width ?: $resource->getWidth(),
-            'height' => $max_height ?: $resource->getHeight(),
+            'width' => $resource->getWidth() ?: $max_width,
+            'height' => $resource->getHeight() ?: $max_height,

This is the key change in this issue and it affects all video providers.

$max_width and $max_height are the values set in the field formatter. These are sent to the oembed provider as the parameters maxwidth and maxheight.

$resource->getWidth() and $resource->getHeight() are the values returned by the video provider, which take into account the values sent with the request.

Before the patch the dimensions of the video were set to exactly the values of $max_width and $max_height.

After the patch, the dimensions are set to the returned values $resource->getWidth() and $resource->getHeight().

It'd be nice to get a few more reviews here.

hudri’s picture

Status: Needs review » Needs work

I've tested patch #27 (can't use patch #34 because it does not apply on Drupal v10.0.x, but code in #34 looks pretty similar). The patch has errors when any dimension field is empty. Error log shows access denied, most likley due missing query parameter values.

Field formatter setting width="1920", height="" causes an error:
Path: /media/oembed?url=https%3A//www.youtube.com/watch.....&max_width=1920&max_height&hash=.....

Field formatter setting width="1920", height="1080" works:
Path: /media/oembed?url=https%3A//www.youtube.com/watch.....&max_width=1920&max_height=1080&hash=.....

UPDATE: This problem was not introduced by this patch, it also happens on an unpatched v10.0.4

imclean’s picture

Status: Needs work » Needs review

#41:

UPDATE: This problem was not introduced by this patch, it also happens on an unpatched v10.0.4

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Retested following #31 and got the whitespace and with the patch I did not.

Also may somewhat be related to #3350299: Allow oEmbed video iFrames to expand to available space

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 2966656-34.patch, failed testing. View results

imclean’s picture

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Random ckeditor error.

Know spokje has been working on the random failures.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 2966656-34.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Appears to be unrelated failure.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs release note

The patch and fix look good, but I think this needs a release note - this may change the size of embedded video on existing sites when they upgrade?

smustgrave’s picture

Issue summary: View changes
Issue tags: -Needs release note

Hiding patches for clarity

Updated issue summary with release note (not great at those)

Moved patch into MR.

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Tests are all green. Since this was previously RTBC going to restore status.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

@nod_ has asked a question that needs answering on the MR.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Tried the suggestion but the caused the tests to fail https://git.drupalcode.org/issue/drupal-2966656/-/jobs/1143627

So reverted back.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Let's add the comment @nod_ is suggesting

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Added comment.

  • nod_ committed 5559dba9 on 11.x
    Issue #2966656 by imclean, smustgrave, phenaproxima, cilefen: Negotiate...

  • nod_ committed 64c99b66 on 10.3.x
    Issue #2966656 by imclean, smustgrave, phenaproxima, cilefen: Negotiate...
nod_’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Thanks

Committed 5559dba and pushed to 11.x. Thanks!

damienmckenna’s picture

For anyone reading the comments via email (and not seeing the merge request updates), the change was also merged to the 10.3.x branch.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.