2

I'm trying to change the cache of my block (hook_block_info_alter) but it seems if I change from CACHE_TEMPORARY to DRUPAL_NO_CACHE, it doesn't make a difference. Drupal declared them like this:

define('CACHE_TEMPORARY', -1); 
define('DRUPAL_NO_CACHE', -1);

Why do we have the same value if they're meant to be different? How can I change the value in hook_block_info_alter?

1 Answer 1

4

They have different uses. CACHE_TEMPORARY and CACHE_PERMANENT are values that cache_set() accepts as its last parameter. DRUPAL_NO_CACHE is used from Drupal only for blocks.

Two variables are allowed to have the same value, and this doesn't create confusion, as long as they are used in different context.

While both the variables have the same value, you should not use them when appropriate, at least because it makes more difficult to upgrade your code to Drupal 8.

In hook_block_info_alter(), as with other hooks for blocks, you should use DRUPAL_NO_CACHE. Keep in mind how the data passed as first argument to the hook is structured. With $blocks['forum']['active']['cache'] = DRUPAL_NO_CACHE; used in hook_block_info_alter(), you would avoid the block showing the active forum posts is cached from Drupal.

2
  • In the hook_block_contents of my module the cache is set to CACHE_TEMPORARY, and when I want to override it on hook_block_info_alter, it already has the "-1" value, does it mean it's already excluded from the cache?
    – GwenM
    Dec 7, 2017 at 15:29
  • 1
    Yes, -1 says the block should not be cached.
    – apaderno
    Dec 8, 2017 at 14:44

Your Answer

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

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