From d2900abc52256190275b1ed7c3d75e88d14c48c4 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Mon, 12 Oct 2015 14:19:06 -0400 Subject: Issue #1534490 by msonnabaum, bigjim, David_Rothstein, pounard, Dave Reid: Make block cache cids alterable --- CHANGELOG.txt | 2 ++ modules/block/block.api.php | 25 +++++++++++++++++++++++++ modules/block/block.module | 1 + 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 98f4bca25..dfdfdd765 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,8 @@ Drupal 7.40, xxxx-xx-xx (development version) ----------------------- +- Added a new hook, hook_block_cid_parts_alter(), to allow modules to alter the + cache keys used for caching a particular block. - Made drupal_set_message() display and return messages when "0" is passed in as the message to set. - Fixed non-functional "Files displayed by default" setting on file fields. diff --git a/modules/block/block.api.php b/modules/block/block.api.php index d7453b24b..e38f7d6ee 100644 --- a/modules/block/block.api.php +++ b/modules/block/block.api.php @@ -363,6 +363,31 @@ function hook_block_list_alter(&$blocks) { } } +/** + * Act on block cache ID (cid) parts before the cid is generated. + * + * This hook allows you to add, remove or modify the custom keys used to + * generate a block cache ID (by default, these keys are set to the block + * module and delta). These keys will be combined with the standard ones + * provided by drupal_render_cid_parts() to generate the final block cache ID. + * + * To change the cache granularity used by drupal_render_cid_parts(), this hook + * cannot be used; instead, set the 'cache' key in the block's definition in + * hook_block_info(). + * + * @params $cid_parts + * An array of elements used to build the cid. + * @param $block + * The block object being acted on. + * + * @see _block_get_cache_id() + */ +function hook_block_cid_parts_alter(&$cid_parts, $block) { + global $user; + // This example shows how to cache a block based on the user's timezone. + $cid_parts[] = $user->timezone; +} + /** * @} End of "addtogroup hooks". */ diff --git a/modules/block/block.module b/modules/block/block.module index 48c80d766..fe04e9902 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -967,6 +967,7 @@ function _block_get_cache_id($block) { // Start with common sub-patterns: block identification, theme, language. $cid_parts[] = $block->module; $cid_parts[] = $block->delta; + drupal_alter('block_cid_parts', $cid_parts, $block); $cid_parts = array_merge($cid_parts, drupal_render_cid_parts($block->cache)); return implode(':', $cid_parts); -- cgit v1.2.3