0

I just made a simlple block by using block > custom block, so I didn't make it programmatically.

I am a Drupal 8 newbie, and I look for a simple and clear solution how to disable caching for that block.

What code should I use ? In what file to put the code ? How to target that unique block ?

Thanks !

0

if you have a custom module let's suppose it called custom_module, your custom_module.module implement hook_preprocess_HOOK.

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function custom_module_preprocess_block(&$vars) {
  //- Change the block-id-name by your block id
  if($vars['derivative_plugin_id'] == 'block-id-name') {
    //- This stops the block being cache in drupal 8
    $vars['#cache']['max-age'] = 0;

  }

Edit :
if you don't know the exact block id you can install Block Content Machine Name module, it will give you the possibility to give a machine name to your custom block, and then implement the hook like the following:

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function custom_module_preprocess_block(&$vars) {
  //- Change the block-machine-name by your block machine name
  if (isset($vars['elements']['content']['#block_content']) &&
    $vars['elements']['content']['#block_content']->get('machine_name')->value == 'block-machine-name'
  ) {
    $vars['#cache']['max-age'] = 0;
  }
}
-1

to complete @berramou's answer : you can do it in theme as well as explained in following answer : https://drupal.stackexchange.com/a/253372/6884

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.