function views_ajax

Same name in other branches
  1. 7.x-3.x 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
Implementation of hook_menu().

File

includes/ajax.inc, line 17

Code

function views_ajax() {
    if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
        $name = $_REQUEST['view_name'];
        $display_id = $_REQUEST['view_display_id'];
        $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
        $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL;
        $dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
        $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
        $ajax_arguments = array(
            'view_name' => $name,
            'view_display_id' => $display_id,
            'view_args' => $_REQUEST['view_args'],
            'view_path' => $path,
            'view_dom_id' => $dom_id,
            'pager_element' => $pager_element,
        );
        views_include('ajax');
        $object = new stdClass();
        $object->status = FALSE;
        $object->display = '';
        $arg = explode('/', $_REQUEST['view_path']);
        if ($arg[0] == 'admin' || variable_get('node_admin_theme', '0') && $arg[0] == 'node' && ($arg[1] == 'add' || $arg[2] == 'edit')) {
            global $custom_theme;
            $custom_theme = variable_get('admin_theme', '0');
            drupal_add_css(drupal_get_path('module', 'system') . '/admin.css', 'module');
        }
        // Load the view.
        if ($view = views_get_view($name)) {
            $view->ajax_path = array(
                $_GET['q'],
                $ajax_arguments,
            );
            if ($view->access($display_id)) {
                // Fix 'q' for paging.
                if (!empty($path)) {
                    $_GET['q'] = $path;
                }
                // Override the display's pager_element with the one actually used.
                if (isset($pager_element)) {
                    $view->display[$display_id]->handler
                        ->set_option('pager_element', $pager_element);
                }
                // Reuse the same DOM id so it matches that in Drupal.settings.
                $view->dom_id = $dom_id;
                $object->status = TRUE;
                $object->display .= $view->preview($display_id, $args);
                // Get the title after the preview call, to let it set
                // up both the view's current display and arguments
                $object->title = $view->get_title();
                // Register the standard JavaScript callback.
                $object->__callbacks = array(
                    'Drupal.Views.Ajax.ajaxViewResponse',
                );
                // Allow other modules to extend the data returned.
                drupal_alter('ajax_data', $object, 'views', $view);
            }
        }
        $messages = theme('status_messages');
        $object->messages = $messages ? '<div class="views-messages">' . $messages . '</div>' : '';
        views_ajax_render($object);
    }
}