You are here

public function XPreviewController::response in BUEditor 8

Same name and namespace in other branches
  1. 8.2 src/Controller/XPreviewController.php \Drupal\bueditor\Controller\XPreviewController::response()

Handles ajax preview requests.

1 string reference to 'XPreviewController::response'
bueditor.routing.yml in ./bueditor.routing.yml
bueditor.routing.yml

File

src/Controller/XPreviewController.php, line 18

Class

XPreviewController
Controller class for ajax preview path.

Namespace

Drupal\bueditor\Controller

Code

public function response(Request $request) {
  $user = $this
    ->currentUser();

  // Check security token for authenticated users.
  if (!$user
    ->isAnonymous()) {
    $token = $request->query
      ->get('token');
    if (!$token || !\Drupal::csrfToken()
      ->validate($token, 'xpreview')) {
      return new JsonResponse([
        'output' => $this
          ->t('Invalid security token.'),
        'status' => FALSE,
      ]);
    }
  }

  // Build output
  $data = [
    'output' => '',
    'status' => TRUE,
  ];

  // Check input
  $input = $request->request
    ->get('input');
  if (is_string($input) && ($input = trim($input)) !== '') {
    $used_format = filter_fallback_format();

    // Check format
    $format = $request->request
      ->get('format');
    if ($format && is_string($format) && $format !== $used_format) {
      if ($format = \Drupal::entityTypeManager()
        ->getStorage('filter_format')
        ->load($format)) {
        if ($format
          ->access('use', $user)) {
          $used_format = $format
            ->id();
        }
      }
    }
    $data['usedFormat'] = $used_format;
    $build = [
      '#type' => 'processed_text',
      '#text' => $input,
      '#format' => $used_format,
    ];
    $data['output'] = '' . \Drupal::service('renderer')
      ->renderPlain($build);
  }
  return new JsonResponse($data);
}