You are here

public function OEmbedResolver::resolveOembed in Gutenberg 8.2

Resolve a URL's oEmbed resource.

Parameters

string $url: The url to resolve an oEmbed for.

int $maxwidth: The maximum width of the oEmbed resource.

Return value

string|null The resolved oEmbed.

Overrides OEmbedResolverInterface::resolveOembed

File

src/OEmbedResolver.php, line 87

Class

OEmbedResolver
Class for resolving oEmbed URLs.

Namespace

Drupal\gutenberg

Code

public function resolveOembed($url, $maxwidth) {
  $output = NULL;
  if ($this->mediaOembedResolver && $this->mediaOembedResourceFetcher) {

    // The media module is enabled. Attempt to use it to resolve the oembed.
    try {
      $resource_url = $this->mediaOembedResolver
        ->getResourceUrl($url, $maxwidth);
      if ($resource_url) {
        $output = $this->mediaOembedResourceFetcher
          ->fetchResource($resource_url)
          ->getHtml();
      }
    } catch (ResourceException $exception) {
      if (!empty($exception
        ->getData()['html'])) {

        // Some might have valid HTML but might be missing certain attributes which result in an exception.
        // e.g. https://streamable.com/ba9f2
        $output = $exception
          ->getData()['html'];
      }
    } catch (ProviderException $exception) {
    }
  }

  // If the media module was unable to resolve it, attempt using the fallback provider (iframe.ly by default).
  if (!$output) {
    $query_params = rawurldecode(UrlHelper::buildQuery([
      'url' => $url,
      'origin' => 'drupal',
      'format' => 'json',
      'maxwidth' => $maxwidth,
    ]));
    $default_provider_uri = $this
      ->getDefaultFallbackOembedProviderUri();
    $arg_separator = strpos($default_provider_uri, '?') === FALSE ? '?' : '&';
    $output = $this
      ->fetchOembedHtml($default_provider_uri . $arg_separator . $query_params);
  }
  return $output;
}