You are here

views_cache_bully.module in Views cache bully 8

Same filename and directory in other branches
  1. 6.3 views_cache_bully.module
  2. 7.3 views_cache_bully.module

Module file for views_cache_bully.

File

views_cache_bully.module
View source
<?php

/**
 * @file
 * Module file for views_cache_bully.
 */

/**
 * Implements hook_menu().
 */
function views_cache_bully_menu() {
  $items = array();
  $items['admin/config/system/views-cache-bully'] = array(
    'title' => 'Views Cache Bully settings',
    'description' => 'Configure default views caching behavior for uncached views.',
    'route_name' => 'views_cache_bully_settings',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

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

/**
 * Implements hook_permission().
 */
function views_cache_bully_permission() {
  return array(
    'administer views cache bully' => array(
      'title' => t('Administer Views Cache Bully'),
      'description' => t('Perform administration tasks for Views Cache Bully.'),
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function views_cache_bully_form_views_ui_admin_settings_advanced_alter(&$form, &$form_state, $form_id) {
  unset($form['cache']['clear_cache']);
  unset($form['cache']['skip_cache']);
  $form['cache']['views_cache_bully'] = array(
    '#markup' => t('Views Cache Bully has disabled these settings.'),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function views_cache_bully_form_views_ui_edit_display_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form_state['section']) && $form_state['section'] == 'cache') {
    $view = $form_state['view'];
    if (!views_cache_bully_view_is_exempt($view)) {
      $form['options']['cache']['type']['#options']['none'] = t('Time-based (from Views Cache Bully)');
    }
    else {
      $form['options']['cache']['type']['#options']['none'] = t('None (exempt from Views Cache Bully)');
    }
  }
}

/**
 * Implements hook_views_plugins_alter().
 */
function views_cache_bully_views_plugins_cache_alter(&$plugins) {
  $plugins['none']['title'] = t('Views Cache Bully');
}

/**
 * Implements hook_views_pre_build().
 */
function views_cache_bully_views_pre_build(ViewExecutable $view) {
  if ($view->display_handler
    ->getPlugin('cache')->definition['id'] == 'none') {
    if (!views_cache_bully_view_is_exempt($view)) {
      $view->display_handler
        ->overrideOption('cache', array(
        'type' => 'time',
        'results_lifespan' => config('views_cache_bully.settings')
          ->get('results_lifespan'),
        'output_lifespan' => config('views_cache_bully.settings')
          ->get('output_lifespan'),
      ));
    }
  }
}

/**
 * Returns flat array of views cache bully exempt views.
 *
 * @param object $view
 *   An initialized view object.
 *
 * @return boolean
 *   TRUE if view should be exempted from bullying.
 */
function views_cache_bully_view_is_exempt($view) {
  $exemptions = array_filter((array) config('views_cache_bully.settings')
    ->get('exemptions'));

  // If administrative settings is enabled, exempt views with exposed filters.
  if (config('views_cache_bully.settings')
    ->get('exempt_exposed')) {
    if (empty($view->inited)) {
      watchdog('views_cache_bully', 'Error, Views Cache Bully could not evaluate view exemption for view @view_name, view was not initialized.', array(
        '@view_name' => $view->name,
      ));
    }
    elseif ($view->display_handler
      ->usesExposed()) {
      return TRUE;
    }
  }

  // If view is in exemption list, return TRUE.
  return in_array($view->storage->id, $exemptions);
}

Functions