You are here

public function OEmbedProxyUrlController::request in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/Controller/OEmbedProxyUrlController.php \Drupal\gutenberg\Controller\OEmbedProxyUrlController::request()

Handle an oEmbed proxy request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

\Drupal\Core\Cache\CacheableJsonResponse The JSON response.

1 string reference to 'OEmbedProxyUrlController::request'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/OEmbedProxyUrlController.php, line 57

Class

OEmbedProxyUrlController
Class OEmbedProxyUrlController.

Namespace

Drupal\gutenberg\Controller

Code

public function request(Request $request) {
  $url = $request->query
    ->get('url');
  $max_width = $request->query
    ->get('maxWidth', 800);
  if (empty($url) || !UrlHelper::isValid($url, TRUE)) {
    throw new BadRequestHttpException('Invalid URL was provided.');
  }

  /*
   * TODO: up for discussion, but the proxy previews won't match the custom oEmbeds settings declared in the
   *  OEmbedProcessor settings. I think the settings should be moved into an API for consolidation.
   */
  $oembed = $this->oembedResolver
    ->resolveOembed($url, $max_width);
  if ($oembed) {
    $cacheable_response = new CacheableJsonResponse([
      'html' => $oembed,
    ]);
    $metadata = new CacheableMetadata();

    // Cache based on the current request uri (includes query strings).
    $metadata
      ->addCacheContexts([
      'url',
    ]);

    // Cache the results for 15 minutes at a time.
    $metadata
      ->setCacheMaxAge(15 * 60);
    $cacheable_response
      ->addCacheableDependency($metadata);
    return $cacheable_response;
  }
  throw new NotFoundHttpException(sprintf('The oEmbed URL (%s) could not be resolved.', $url));
}