Pagri
Pagri

Reputation: 290

Drupal 8: Turn off a specific view's block cache

I wanted to know how to turn off the block caching for all blocks which were created by a view. I would like to do this from my theme if that is possible. My preliminary research suggested that using hook_block_view_BASE_BLOCK_ID_alter() might be able to accomplish this.

The view I would like to turn off caching for has a machine name of background_image. My theme is named my_theme. I tried this in my_theme.theme but it did not work:

/*
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function my_theme_block_view_background_image_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) { 
  $build['#cache']['max-age'] = 0;
}

I also tried to use a callback:

/*
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function my_theme_block_view_background_image_alter(array &$build, BlockPluginInterface $block) {
  $build['#pre_render'][] = '_background_image_block_pre_render';
}

/**
 * Pre-render callback
 */
function _background_image_block_pre_render(array $build) {
  $build = array(
    '#cache' => array('max-age' => 0),
  );
  return $build;
}

Thanks in advance.

Upvotes: 0

Views: 1889

Answers (2)

gangadhara
gangadhara

Reputation: 11

In Drupal 8 in order to disable cache for a specific view

your view > ADVANCED > Caching > None

Upvotes: 0

Pagri
Pagri

Reputation: 290

I discovered that I am able to turn off the view's block caching within the Views UI. It is under Advanced. Turning it off there fixed my problem

Upvotes: 2

Related Questions