1

I read a lot of posts on how to disable caching on a custom block but none seemed to give the perfect solution.

  1. $build['#cache'] = ['max-age' => 0] or public function getCacheMaxAge() { return 0; }: Do not seem to work, or at least for authenticated users
  2. \Drupal::service('page_cache_kill_switch')->trigger(): Kill the complete page cache, not only the block ones
  3. Lazy builders: Seems to work only for anonymous users

In my case, I have a block which renders a random node and it's clearly an issue while I'm not able to refresh this cache.

So how to set up such a block? (Drupal 8.4.3)

Posts giving not working solutions:


Edit:

public function build() {
    [...]
    if ($prev_node) {
      $render_controller = \Drupal::entityTypeManager()
        ->getViewBuilder($prev_node->getEntityTypeId());
      $build = $render_controller->view($prev_node, 'pagination');
      $build['#node']->field_custom = 'something';
    }
    return $build;
}

public function getCacheMaxAge() {
    return 0;
}

Thank's to @Clive, it seems my issue is not linked to the block cache itself, but to the entity I render in this block. If so, I have no ideas how on to resolve this issue.

After many searches, D7 cache_clear_all('field:node:10', 'my_field') function seems to do what I want to do. What is its equivalent for D8 ?

  • 1
    Possible duplicate of How to prevent a block from being cached? – No Sssweat Feb 15 '18 at 13:44
  • I know it is a duplicate, the fact is this post does not provide a solution for this particular issue. I've been asked in another post to create a new one to provide one with, I hope, a final solution. – Pauloscorps Feb 15 '18 at 13:47
  • Yes, that's why I want to disable caching for this specific block only. I have also a prev / next pagination block who loads the string "Previous" or "Next", and those strings are cached, so, on the next page, the "next" button become "previous". So, I really need to implement this for 2 blocks. – Pauloscorps Feb 15 '18 at 14:13
  • What you've done with the max-age is correct: drupal.org/docs/8/api/cache-api/cache-max-age it might be your browser just caching the page and thats why it appears to be cached by drupal still. Try with the inspector tools open and caching disabled – Leigh Feb 15 '18 at 14:13
  • @LeighMason I had a huge doubt on your suggestion but I gave it a try, and it does not work. The only way to view my block correctly is to flush Drupal cache. – Pauloscorps Feb 15 '18 at 14:16
2

Finally found a solution, thanks to MXH on the Drupal Slack. I had to clear the render array cache like this:

$render_controller = \Drupal::entityTypeManager()
        ->getViewBuilder($node->getEntityTypeId());
$items['next'] = $render_controller->view($node, 'pagination');
$items['next']['#cache'] = ['disabled' => TRUE];
$items['next']['#node']->my_field = t('some text');
1

I got the same issue that you, here is what it worked for me.

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function MY_MODULE_preprocess_block(&$variables) {
  $blocks_to_cache = [
    'SOME_block',
  ];
  if (in_array($variables['base_plugin_id'], $blocks_to_cache)) {
    $variables['#cache']['max-age'] = 0;
  }

}

So, In my my_module.module I search if I'm displaying this block, then I clean the cache for that block. You can several block names.

  • Thank you Carlos, but sadly, I tested it and it does not work for me. – Pauloscorps Feb 16 '18 at 9:00
0

Found the solution here - https://stefvanlooveren.me/blog/disable-block-caching-drupal-8

Add the below code to your block code file

public function getCacheMaxAge() { return 0; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.