You are here

public function AdvancedInsertViewController::ajaxView in Advanced Insert View 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/AdvancedInsertViewController.php \Drupal\insert_view_adv\Controller\AdvancedInsertViewController::ajaxView()

Loads and renders a view via AJAX.

Parameters

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

\Drupal\filter\Entity\FilterFormat|null $filter_format: The filter format object.

Return value

\Symfony\Component\HttpFoundation\Response The view response as ajax response.

1 string reference to 'AdvancedInsertViewController::ajaxView'
insert_view_adv.routing.yml in ./insert_view_adv.routing.yml
insert_view_adv.routing.yml

File

src/Controller/AdvancedInsertViewController.php, line 61

Class

AdvancedInsertViewController
Class AdvancedInsertViewController.

Namespace

Drupal\insert_view_adv\Controller

Code

public function ajaxView(Request $request, FilterFormat $filter_format = NULL) {
  $name = $request->query
    ->get('view_name');
  $display_id = $request->query
    ->get('view_display_id');
  if (isset($name) && isset($display_id)) {
    $args = $request->query
      ->get('view_args');
    $args = isset($args) && $args !== '' ? explode('/', $args) : [];

    // Arguments can be empty, make sure they are passed on as NULL so that
    // argument validation is not triggered.
    $args = array_map(function ($arg) {
      return $arg == '' ? NULL : $arg;
    }, $args);
    if ($args) {

      // Transform the arguments back to string.
      $args = implode('/', $args);
    }
    $context = new RenderContext();
    $configuration = $filter_format
      ->filters('insert_view_adv')
      ->getConfiguration();
    $configuration = Json::encode($configuration);
    $preview = $this->renderer
      ->executeInRenderContext($context, function () use ($name, $display_id, $args, $configuration) {
      return InsertView::build($name, $display_id, $args, $configuration);
    });
    if (!$context
      ->isEmpty() && !empty($preview)) {
      $bubbleable_metadata = $context
        ->pop();
      BubbleableMetadata::createFromRenderArray($preview)
        ->merge($bubbleable_metadata)
        ->applyTo($preview);
    }
    $html = $this->renderer
      ->renderPlain($preview);
    return (new Response($html, 200))
      ->setPrivate()
      ->setMaxAge(300);
  }
  else {
    throw new NotFoundHttpException();
  }
}