diff --git a/core/modules/media/src/OEmbed/UrlResolver.php b/core/modules/media/src/OEmbed/UrlResolver.php index 5985e6874ada5b60dc875b0eb053ed0ceeee42b7..c8c2d4ee48cd4b9f4cbf87d0a6e4fd07e0ef67b5 100644 --- a/core/modules/media/src/OEmbed/UrlResolver.php +++ b/core/modules/media/src/OEmbed/UrlResolver.php @@ -158,10 +158,8 @@ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) { } $provider = $this->getProviderByUrl($url); - $endpoints = $provider->getEndpoints(); - $endpoint = reset($endpoints); - $resource_url = $endpoint->buildResourceUrl($url); + $resource_url = $this->getEndpointMatchingUrl($url, $provider); $parsed_url = UrlHelper::parse($resource_url); if ($max_width) { $parsed_url['query']['maxwidth'] = $max_width; @@ -181,4 +179,27 @@ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) { return $resource_url; } + /** + * For the given media item URL find an endpoint with schemes that match. + * + * @param string $url + * The media URL used to lookup the matching endpoint. + * @param \Drupal\media\OEmbed\Provider $provider + * The oEmbed provider for the asset. + * + * @return string + * The resource url. + */ + protected function getEndpointMatchingUrl($url, Provider $provider) { + $endpoints = $provider->getEndpoints(); + $resource_url = reset($endpoints)->buildResourceUrl($url); + foreach ($endpoints as $endpoint) { + if ($endpoint->matchUrl($url)) { + $resource_url = $endpoint->buildResourceUrl($url); + break; + } + } + return $resource_url ?? reset($endpoints)->buildResourceUrl($url); + } + } diff --git a/core/modules/media/tests/fixtures/oembed/providers.json b/core/modules/media/tests/fixtures/oembed/providers.json index 3ff7de23c70b26be760dc1aed1c43596b02f097e..9791d7378dc5b3f0559ecb03ef001df3560135d7 100644 --- a/core/modules/media/tests/fixtures/oembed/providers.json +++ b/core/modules/media/tests/fixtures/oembed/providers.json @@ -72,5 +72,35 @@ "discovery": true } ] + }, + { + "provider_name": "Facebook", + "provider_url": "https:\/\/www.facebook.com\/", + "endpoints": [ + { + "schemes": [ + "https:\/\/www.facebook.com\/*\/posts\/*", + "https:\/\/www.facebook.com\/photos\/*", + "https:\/\/www.facebook.com\/*\/photos\/*", + "https:\/\/www.facebook.com\/photo.php*", + "https:\/\/www.facebook.com\/photo.php", + "https:\/\/www.facebook.com\/*\/activity\/*", + "https:\/\/www.facebook.com\/permalink.php", + "https:\/\/www.facebook.com\/media\/set?set=*", + "https:\/\/www.facebook.com\/questions\/*", + "https:\/\/www.facebook.com\/notes\/*\/*\/*" + ], + "url": "https:\/\/www.facebook.com\/plugins\/post\/oembed.json", + "discovery": true + }, + { + "schemes": [ + "https:\/\/www.facebook.com\/*\/videos\/*", + "https:\/\/www.facebook.com\/video.php" + ], + "url": "https:\/\/www.facebook.com\/plugins\/video\/oembed.json", + "discovery": true + } + ] } ] diff --git a/core/modules/media/tests/src/Functional/UrlResolverTest.php b/core/modules/media/tests/src/Functional/UrlResolverTest.php index ff826c24cb73cd7edd39b2644f60f439d8351a2a..7668d69357d22d6e4fc1cfaa36d294ce8a6baf7d 100644 --- a/core/modules/media/tests/src/Functional/UrlResolverTest.php +++ b/core/modules/media/tests/src/Functional/UrlResolverTest.php @@ -50,6 +50,10 @@ public function providerEndpointMatching() { 'http://www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime', 'http://www.collegehumor.com/oembed.json?url=http://www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime', ], + 'match by endpoint: Facebook' => [ + 'https://www.facebook.com/facebook/videos/10153231379946729/', + 'https://www.facebook.com/plugins/video/oembed.json?url=https://www.facebook.com/facebook/videos/10153231379946729/', + ], ]; }