You are here

authcache_views.module in Authenticated User Page Caching (Authcache) 7.2

Authcache support for views module.

File

modules/authcache_views/authcache_views.module
View source
<?php

/**
 * @file
 * Authcache support for views module.
 */

/**
 * Implements hook_authcache_p13n_fragment().
 */
function authcache_views_authcache_p13n_fragment() {
  $views = views_get_all_views();
  $fragments = array();
  foreach ($views as $view_name => $view) {
    foreach ($view->display as $display_id => $display) {
      $config = _authcache_views_view_get_options($view, $display_id);
      if (!empty($config['status'])) {
        $config += authcache_p13n_config_defaults();
        $fragment_id = "views/{$view->name}/{$display_id}";
        $granularity = authcache_p13n_config_cache_granularity($config);
        $fragments[$fragment_id] = array(
          'fragment' => array(
            '#class' => 'AuthcacheViewsFragment',
            '#arguments' => array(
              $view->name,
              $display_id,
            ),
          ),
          'bootstrap phase' => DRUPAL_BOOTSTRAP_FULL,
          'cache maxage' => authcache_p13n_config_cache_maxage($config),
          'cache granularity' => $granularity,
          'admin entry object' => array(
            '#class' => 'AuthcacheViewsAdminEntry',
            '#arguments' => array(
              $view_name,
              $display_id,
              $config,
              '@admin entry',
            ),
          ),
        );

        // If this is a page-display and cache-granularity is set to per-page,
        // add a context provider for deriving views arguments from request
        // parameters.
        if ($display->display_plugin === 'page' && $granularity & AuthcacheP13nCacheGranularity::PER_PAGE) {
          $fragments[$fragment_id]['views arguments'] = array(
            '#class' => 'AuthcacheViewsDisplayPageContextProvider',
            '#member_of' => 'context providers',
          );
        }
      }
    }
  }
  return $fragments;
}

/**
 * Implements hook_authcache_p13n_admin_groups().
 */
function authcache_views_authcache_p13n_admin_groups() {
  if (module_exists('views_ui')) {
    return array(
      t('Views') => t('To add a view to the list of personalized views, visit the <a href="!views_url">views administration page</a> and click "configure" next to the view in question.', array(
        '!views_url' => url('admin/structure/views'),
      )),
    );
  }
}

/**
 * Implements hook_views_api().
 */
function authcache_views_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_views_plugins().
 */
function authcache_views_views_plugins() {
  $plugins['display_extender']['authcache_views'] = array(
    'title' => t('Authcache'),
    'help' => t('Use ESI or Ajax to display views with personalized content on cached pages'),
    'handler' => 'authcache_views_plugin_display_extender',
  );
  return $plugins;
}

/**
 * Implements hook_views_pre_view().
 */
function authcache_views_views_pre_view(&$view, &$display_id, &$args) {

  // Enforce a static dom-id for views loaded by authcache.
  $config = _authcache_views_view_get_options($view, $view->current_display);
  if (!empty($config['status'])) {
    $view->dom_id = drupal_html_class("authcache-{$view->name}-{$view->current_display}");
  }
}

/**
 * Implements hook_views_post_render().
 */
function authcache_views_views_post_render(&$view, &$output, &$cache) {
  if (authcache_page_is_cacheable()) {
    $config = _authcache_views_view_get_options($view, $view->current_display);
    if (!empty($config['status'])) {
      $fragment_id = "views/{$view->name}/{$view->current_display}";
      $fragment = array(
        '#theme' => 'authcache_p13n_fragment',
        '#fragment' => $fragment_id,
        '#clients' => $config['clients'],
        '#fallback' => $config['fallback'],
      );
      $element = array(
        '#markup' => $output,
      );
      authcache_p13n_attach($element, $fragment);
      $output = render($element);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see https://drupal.org/node/551808
 */
function authcache_views_form_views_ui_edit_form_alter(&$form, &$form_state) {
  $form['actions']['save']['#submit'][] = 'authcache_p13n_request_router_rebuild';
}

/**
 * Return options for the given view.
 */
function _authcache_views_view_get_options($view, $display_name) {
  if ($view->disabled) {
    return;
  }
  $display_options = $view->display[$display_name]->display_options;
  if (empty($display_options['authcache'])) {
    return;
  }
  return $display_options['authcache'];
}