Skip to content

Instantly share code, notes, and snippets.

@amcgowanca
Created February 4, 2014 14:17
Show Gist options
  • Save amcgowanca/8804376 to your computer and use it in GitHub Desktop.
Save amcgowanca/8804376 to your computer and use it in GitHub Desktop.
Turn default caching and "use slave" option on for Views 3 in Drupal 7.
/**
* Implements hook_views_default_views_alter().
*/
function MODULENAME_views_default_views_alter(&$views) {
foreach ($views as $view_name => &$view) {
// If the view is disabled, simply continue onto the next.
if (TRUE == $view->disabled) {
continue;
}
// For each of the displays available for this `$view`, turn on slave option and cache.
foreach ($view->display as $display_name => &$display) {
// Each view should have the ability to use the Slave database
// if it is available. Enabling this option as this will provide this ability.
$views[$view_name]->display[$display_name]->display_options['query']['options']['slave'] = TRUE;
// Turn on default caching as type `time` with results and output
// cached for a total of one hour (3600 seconds).
if ('none' == $display->display_options['cache']['type']) {
$views[$view_name]->display[$display_name]->display_options['cache']['type'] = 'time';
$views[$view_name]->display[$display_name]->display_options['cache']['results_lifespan'] = '3600';
$views[$view_name]->display[$display_name]->display_options['cache']['results_lifespan_custom'] = '0';
$views[$view_name]->display[$display_name]->display_options['cache']['output_lifespan'] = '3600';
$views[$view_name]->display[$display_name]->display_options['cache']['output_lifespan_custom'] = '0';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment