You are here

function views_ajax in Views (for Drupal 7) 8.3

Same name and namespace in other branches
  1. 6.3 includes/ajax.inc \views_ajax()
  2. 6.2 includes/ajax.inc \views_ajax()
  3. 7.3 includes/ajax.inc \views_ajax()

Menu callback to load a view via AJAX.

Related topics

1 string reference to 'views_ajax'
views_menu in ./views.module
Implement hook_menu().

File

includes/ajax.inc, line 19
Handles the server side AJAX interactions of Views.

Code

function views_ajax() {
  $request = drupal_container()
    ->get('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();
    $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;
    $commands = array();

    // 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',
      'ajax_html_ids',
      'ajax_page_state',
    ) as $key) {
      if ($request->query
        ->has($key)) {
        $request->query
          ->remove($key);
      }
      if ($request->request
        ->has($key)) {
        $request->request
          ->remove($key);
      }
    }

    // Load the view.
    $view = views_get_view($name);
    if ($view && $view
      ->access($display_id)) {

      // Fix the current path for paging.
      if (!empty($path)) {
        _current_path($path);
      }

      // 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 drupal_get_destination()
      $origin_destination = $path;
      $query = drupal_http_build_query($request->query
        ->all());
      if ($query != '') {
        $origin_destination .= '?' . $query;
      }
      $destination =& drupal_static('drupal_get_destination');
      $destination = array(
        'destination' => $origin_destination,
      );

      // Override the display's pager_element with the one actually used.
      if (isset($pager_element)) {
        $commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
        $view->displayHandlers[$display_id]
          ->setOption('pager_element', $pager_element);
      }

      // Reuse the same DOM id so it matches that in Drupal.settings.
      $view->dom_id = $dom_id;
      $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $view
        ->preview($display_id, $args));
    }
    drupal_alter('views_ajax_data', $commands, $view);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
}