From f87d33acb4d1fb21496673c2aa76f9669f349bb4 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Wed, 28 Jul 2021 17:00:22 +1000
Subject: [PATCH] Issue #2966043 by phenaproxima, owenbush, a.dmitriiev,
 marcoscano, tapscolaM, xtineroque, ramya.shankaralingam: Handle oEmbed
 resources without an explicit height

---
 core/modules/media/src/OEmbed/Resource.php           | 12 ++++++------
 .../media/tests/fixtures/oembed/rich_twitter.json    |  2 +-
 .../FieldFormatter/OEmbedFormatterTest.php           |  6 +++++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/core/modules/media/src/OEmbed/Resource.php b/core/modules/media/src/OEmbed/Resource.php
index ac3e7510cd5a..983f08df28d3 100644
--- a/core/modules/media/src/OEmbed/Resource.php
+++ b/core/modules/media/src/OEmbed/Resource.php
@@ -236,7 +236,7 @@ public static function link($url = NULL, Provider $provider = NULL, $title = NUL
    * @param int $width
    *   The width of the photo, in pixels.
    * @param int $height
-   *   The height of the photo, in pixels.
+   *   (optional) The height of the photo, in pixels.
    * @param \Drupal\media\OEmbed\Provider $provider
    *   (optional) The resource provider.
    * @param string $title
@@ -260,7 +260,7 @@ public static function link($url = NULL, Provider $provider = NULL, $title = NUL
    *
    * @return static
    */
-  public static function photo($url, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+  public static function photo($url, $width, $height = NULL, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
     if (empty($url)) {
       throw new \InvalidArgumentException('Photo resources must provide a URL.');
     }
@@ -280,7 +280,7 @@ public static function photo($url, $width, $height, Provider $provider = NULL, $
    * @param int $width
    *   The width of the resource, in pixels.
    * @param int $height
-   *   The height of the resource, in pixels.
+   *   (optional) The height of the resource, in pixels.
    * @param \Drupal\media\OEmbed\Provider $provider
    *   (optional) The resource provider.
    * @param string $title
@@ -304,7 +304,7 @@ public static function photo($url, $width, $height, Provider $provider = NULL, $
    *
    * @return static
    */
-  public static function rich($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+  public static function rich($html, $width, $height = NULL, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
     if (empty($html)) {
       throw new \InvalidArgumentException('The resource must provide an HTML representation.');
     }
@@ -325,7 +325,7 @@ public static function rich($html, $width, $height, Provider $provider = NULL, $
    * @param int $width
    *   The width of the video, in pixels.
    * @param int $height
-   *   The height of the video, in pixels.
+   *   (optional) The height of the video, in pixels.
    * @param \Drupal\media\OEmbed\Provider $provider
    *   (optional) The resource provider.
    * @param string $title
@@ -349,7 +349,7 @@ public static function rich($html, $width, $height, Provider $provider = NULL, $
    *
    * @return static
    */
-  public static function video($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+  public static function video($html, $width, $height = NULL, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
     $resource = static::rich($html, $width, $height, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
     $resource->type = self::TYPE_VIDEO;
 
diff --git a/core/modules/media/tests/fixtures/oembed/rich_twitter.json b/core/modules/media/tests/fixtures/oembed/rich_twitter.json
index d62ce57a88f1..17bbe1f21e56 100644
--- a/core/modules/media/tests/fixtures/oembed/rich_twitter.json
+++ b/core/modules/media/tests/fixtures/oembed/rich_twitter.json
@@ -4,7 +4,7 @@
   "author_url": "https:\/\/twitter.com\/drupaldevdays",
   "html": "<h1>By the power of Grayskull, Twitter works!</h1>",
   "width": 550,
-  "height": 360,
+  "height": null,
   "type": "rich",
   "cache_age": "3153600000",
   "provider_name": "Twitter",
diff --git a/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
index e8d9c988b47e..53f4b40e67dd 100644
--- a/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
+++ b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
@@ -98,7 +98,11 @@ public function providerRender() {
       'tweet' => [
         'https://twitter.com/drupaldevdays/status/935643039741202432',
         'rich_twitter.json',
-        [],
+        [
+          // The tweet resource does not specify a height, so the formatter
+          // should default to the configured maximum height.
+          'max_height' => 360,
+        ],
         [
           'iframe' => [
             'src' => '/media/oembed?url=https%3A//twitter.com/drupaldevdays/status/935643039741202432',
-- 
GitLab