You are here

public function ViewsAjaxController::ajaxView in Views Ajax Get 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/ViewsAjaxController.php \Drupal\views_ajax_get\Controller\ViewsAjaxController::ajaxView()

Loads and renders a view via AJAX.

Parameters

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

Return value

\Drupal\views\Ajax\ViewAjaxResponse The view response as ajax response.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the view was not found.

Overrides ViewAjaxController::ajaxView

File

src/Controller/ViewsAjaxController.php, line 18

Class

ViewsAjaxController
Controller that allows for Views AJAX GET requests that can be cached.

Namespace

Drupal\views_ajax_get\Controller

Code

public function ajaxView(Request $request) {
  if ($request
    ->getMethod() !== Request::METHOD_GET) {
    return parent::ajaxView($request);
  }

  // Add all query parameters to the post variable, because this is what
  // \Drupal\views\Controller\ViewAjaxController::ajaxView expects.
  $request->request
    ->add($request->query
    ->all());
  $response = parent::ajaxView($request);
  $view = $response
    ->getView();
  if (_views_ajax_get_is_ajax_get_view($view)) {
    $cacheable_response = new CacheableViewsAjaxResponse();
    $cacheable_response
      ->setView($view);
    $cacheable_commands =& $cacheable_response
      ->getCommands();
    $cacheable_commands = $response
      ->getCommands();
    $view_metadata = CacheableMetadata::createFromRenderArray($view->element);
    $metadata = $cacheable_response
      ->getCacheableMetadata();
    $metadata
      ->addCacheableDependency($view_metadata);

    // Don't allow attachments
    // $cacheable_response->setAttachments($response->getAttachments());
    return $cacheable_response;
  }
  return $response;
}