1

I have an alternative main menu that is used on specific node types. When turning on the cache the menu block is cached. The theme suggestion for the menu is then ignored.

I tried to disable the menu cache by changing the block preprocess cache settings. But the menu block is not called when using hook_preprocess_block.

Also I tried to change the menu / block by using the hook_page_preprocess

function bootstrap_custom_preprocess_page(&$variables) {
   //disable caches for every top header element
   //we do this as the dynamic header menu can't be cached - because of the landing page node type

   if(key_exists('top_header', $variables['page'])){
      foreach ($variables['page']['top_header'] as $key => $value){
        if(substr($key, 0, 1) !== '#'){
          $value['#cache']['max-age'] = 0;
        }
      }
   }
}

No success.

1 Answer 1

0
function bootstrap_custom_preprocess_block(&$vars) {
    if($vars['derivative_plugin_id'] == 'header-navigation') {
        //-- This stops the block being cached in drupal 8
        $vars['#cache']['max-age'] = 0;
    }
}

This works for me. Block preprocess is correct.

1
  • 3
    Instead of disabling the cache you can set a cache context. There is no context for the node type, but you can for example try one for the path: $variables['#cache']['contexts'][] = 'url.path.parent';
    – 4uk4
    Feb 23, 2018 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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