ViewAjaxController::ajaxView

public ViewAjaxController::ajaxView(Request $request)

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.

File

core/modules/views/src/Controller/ViewAjaxController.php, line 112

Class

ViewAjaxController
Defines a controller to load a view via AJAX.

Namespace

Drupal\views\Controller

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public function ajaxView(Request $request) {
  $name = $request->request->get('view_name');
  $display_id = $request->request->get('view_display_id');
  if (isset($name) && isset($display_id)) {
    $args = $request->request->get('view_args');
    $args = isset($args) && $args !== '' ? explode('/', $args) : array();
 
    // 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);
 
    $path = $request->request->get('view_path');
    $dom_id = $request->request->get('view_dom_id');
    $dom_id = isset($dom_id) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $dom_id) : NULL;
    $pager_element = $request->request->get('pager_element');
    $pager_element = isset($pager_element) ? intval($pager_element) : NULL;
 
    $response = new ViewAjaxResponse();
 
    // Remove all of this stuff from the query of the request so it doesn't
    // end up in pagers and tablesort URLs.
    foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER) as $key) {
      $request->query->remove($key);
      $request->request->remove($key);
    }
 
    // Load the view.
    if (!$entity = $this->storage->load($name)) {
      throw new NotFoundHttpException();
    }
    $view = $this->executableFactory->get($entity);
    if ($view && $view->access($display_id)) {
      $response->setView($view);
      // Fix the current path for paging.
      if (!empty($path)) {
        $this->currentPath->setPath('/' . $path, $request);
      }
 
      // Add all POST data, because AJAX is always a post and many things,
      // such as tablesorts, exposed filters and paging assume GET.
      $request_all = $request->request->all();
      $query_all = $request->query->all();
      $request->query->replace($request_all + $query_all);
 
      // Overwrite the destination.
      // @see the redirect.destination service.
      $origin_destination = $path;
 
      // Remove some special parameters you never want to have part of the
      // destination query.
      $used_query_parameters = $request->query->all();
      // @todo Remove this parsing once these are removed from the request in
      unset($used_query_parameters[FormBuilderInterface::AJAX_FORM_REQUEST], $used_query_parameters[MainContentViewSubscriber::WRAPPER_FORMAT], $used_query_parameters['ajax_page_state']);
 
      $query = UrlHelper::buildQuery($used_query_parameters);
      if ($query != '') {
        $origin_destination .= '?' . $query;
      }
      $this->redirectDestination->set($origin_destination);
 
      // Override the display's pager_element with the one actually used.
      if (isset($pager_element)) {
        $response->addCommand(new ScrollTopCommand(".js-view-dom-id-$dom_id"));
        $view->displayHandlers->get($display_id)->setOption('pager_element', $pager_element);
      }
      // Reuse the same DOM id so it matches that in drupalSettings.
      $view->dom_id = $dom_id;
 
      $context = new RenderContext();
      $preview = $this->renderer->executeInRenderContext($context, function() use ($view, $display_id, $args) {
        return $view->preview($display_id, $args);
      });
      if (!$context->isEmpty()) {
        $bubbleable_metadata = $context->pop();
        BubbleableMetadata::createFromRenderArray($preview)
          ->merge($bubbleable_metadata)
          ->applyTo($preview);
      }
      $response->addCommand(new ReplaceCommand(".js-view-dom-id-$dom_id", $preview));
 
      return $response;
    }
    else {
      throw new AccessDeniedHttpException();
    }
  }
  else {
    throw new NotFoundHttpException();
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.