function MediaFilterController::preview

Same name and namespace in other branches
  1. 8.9.x core/modules/media/src/Controller/MediaFilterController.php \Drupal\media\Controller\MediaFilterController::preview()
  2. 10 core/modules/media/src/Controller/MediaFilterController.php \Drupal\media\Controller\MediaFilterController::preview()
  3. 11.x 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 97

Class

MediaFilterController
Controller which renders a preview of the provided text.

Namespace

Drupal\media\Controller

Code

public function preview(Request $request, FilterFormatInterface $filter_format) {
    self::checkCsrf($request, \Drupal::currentUser());
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.