You are here

public function MediaFilterController::preview in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/Controller/MediaFilterController.php \Drupal\media\Controller\MediaFilterController::preview()

Returns a HTML response containing a preview of the text after filtering.

Applies all of the given text format's filters, not just the `media_embed` filter, because for example `filter_align` and `filter_caption` may apply to it as well.

Parameters

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

\Drupal\filter\FilterFormatInterface $filter_format: The text format.

Return value

\Symfony\Component\HttpFoundation\Response The filtered text.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Throws an exception if 'text' parameter is not found in the query string.

See also

\Drupal\editor\EditorController::getUntransformedText

1 call to MediaFilterController::preview()
TestMediaFilterController::preview in core/modules/media/tests/modules/media_test_embed/src/Controller/TestMediaFilterController.php
Returns a HTML response containing a preview of the text after filtering.
1 string reference to 'MediaFilterController::preview'
media.routing.yml in core/modules/media/media.routing.yml
core/modules/media/media.routing.yml
1 method overrides MediaFilterController::preview()
TestMediaFilterController::preview in core/modules/media/tests/modules/media_test_embed/src/Controller/TestMediaFilterController.php
Returns a HTML response containing a preview of the text after filtering.

File

core/modules/media/src/Controller/MediaFilterController.php, line 95

Class

MediaFilterController
Controller which renders a preview of the provided text.

Namespace

Drupal\media\Controller

Code

public function preview(Request $request, FilterFormatInterface $filter_format) {
  $text = $request->query
    ->get('text');
  $uuid = $request->query
    ->get('uuid');
  if ($text == '' || $uuid == '') {
    throw new NotFoundHttpException();
  }
  $build = [
    '#type' => 'processed_text',
    '#text' => $text,
    '#format' => $filter_format
      ->id(),
  ];
  $html = $this->renderer
    ->renderPlain($build);

  // Load the media item so we can embed the label in the response, for use
  // in an ARIA label.
  $headers = [];
  if ($media = $this->entityRepository
    ->loadEntityByUuid('media', $uuid)) {
    $headers['Drupal-Media-Label'] = $this->entityRepository
      ->getTranslationFromContext($media)
      ->label();
  }

  // Note that we intentionally do not use:
  // - \Drupal\Core\Cache\CacheableResponse because caching it on the server
  //   side is wasteful, hence there is no need for cacheability metadata.
  // - \Drupal\Core\Render\HtmlResponse because there is no need for
  //   attachments nor cacheability metadata.
  return (new Response($html, 200, $headers))
    ->setPrivate()
    ->setMaxAge(300);
}