You are here

views_ajax_history.module in Views AJAX History 8

Same filename and directory in other branches
  1. 7 views_ajax_history.module

Add bookmarking abilities to AJAX Views.

File

views_ajax_history.module
View source
<?php

/**
 * @file
 * Add bookmarking abilities to AJAX Views.
 */
use Drupal\views\ViewExecutable;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function views_ajax_history_help($route_name, RouteMatchInterface $arg) {
  switch ($route_name) {
    case 'help.page.views_ajax_history':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Enable bookmaking of AJAX views. Supports filters and paging.') . '</p>';

      // Add a link to the Drupal.org project.
      $output .= '<p>';
      $output .= t('Visit the <a href=":project_link">Views AJAX History project pages</a> on Drupal.org for more information.', [
        ':project_link' => 'https://www.drupal.org/project/views_ajax_history',
      ]);
      $output .= '</p>';
      return $output;
  }
}

/**
 * Implements hook_views_pre_render().
 */
function views_ajax_history_views_pre_render(ViewExecutable $view) {
  $display_extenders_options = $view->display_handler
    ->getOption('display_extenders');
  if ($view
    ->ajaxEnabled() && (isset($display_extenders_options['ajax_history']['enable_history']) && $display_extenders_options['ajax_history']['enable_history'] == TRUE) && empty($view->is_attachment) && empty($view->live_preview)) {

    // @TODO add option to views form for html4+html5 or html5 only
    $view->element['#attached']['library'][] = 'views_ajax_history/history';
    $view->element['#attached']['drupalSettings']['viewsAjaxHistory'] = [
      'renderPageItem' => \Drupal::service('pager.parameters')
        ->findPage(),
    ];
    $view->element['#cache']['contexts'][] = 'url.query_args.pagers';
  }
}