You are here

public function EmbedController::preview in Embed 8

Returns an Ajax response to generate preview of embedded items.

Expects the the HTML element as GET parameter.

Parameters

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

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

Return value

\Symfony\Component\HttpFoundation\Response The preview of the embedded item specified by the data attributes.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Throws an exception if 'value' parameter is not found in the request.

1 call to EmbedController::preview()
EmbedController::previewEditor in src/Controller/EmbedController.php
Returns an Ajax response to generate preview of an entity.
1 string reference to 'EmbedController::preview'
embed.routing.yml in ./embed.routing.yml
embed.routing.yml

File

src/Controller/EmbedController.php, line 68

Class

EmbedController
Returns responses for Embed module routes.

Namespace

Drupal\embed\Controller

Code

public function preview(Request $request, FilterFormatInterface $filter_format) {
  $text = $request
    ->get('text') ?: $request
    ->get('value');
  if (empty($text)) {
    throw new NotFoundHttpException();
  }
  $build = [
    '#type' => 'processed_text',
    '#text' => $text,
    '#format' => $filter_format
      ->id(),
    '#langcode' => $this
      ->languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId(),
  ];
  if ($this
    ->isAjax()) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new EmbedInsertCommand($build));
    return $response;
  }
  else {
    $html = $this->renderer
      ->renderPlain($build);

    // 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))
      ->setPrivate()
      ->setMaxAge(300);
  }
}