From 5555343584e09aebe75e52e21fea25271266b80a Mon Sep 17 00:00:00 2001
From: Moshe Weitzman <weitzman@tejasa.com>
Date: Sun, 24 Apr 2011 22:49:35 -0400
Subject: [PATCH] Block cache is now implemented as a standard drupal_render() #cache, and not its own custom universe.

---
 modules/block/block.module         |  178 ++++++++++++++---------------------
 modules/block/block.test           |    3 -
 modules/dashboard/dashboard.module |    3 +-
 3 files changed, 72 insertions(+), 112 deletions(-)

diff --git a/modules/block/block.module b/modules/block/block.module
index c5e229b..92dd63b 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -330,8 +330,40 @@ function _block_get_renderable_array($list = array()) {
   $weight = 0;
   $build = array();
   foreach ($list as $key => $block) {
-    $build[$key] = $block->content;
-    unset($block->content);
+    $build[$key] = array(
+      '#block' => $block,
+      '#weight' => ++$weight,
+      '#theme_wrappers' => array('block'),
+    );
+
+    /* Block caching is not compatible with node_access modules. We also
+     * preserve the submission of forms in blocks, by fetching from cache
+     * only if the request method is 'GET' (or 'HEAD'). User 1 being out of the
+     * regular 'roles define permissions' schema, it brings too many chances of
+     * having unwanted output get in the cache and later be served to other
+     * users. We therefore exclude user 1 from block caching.
+     */
+    if (
+      $GLOBALS['user']->uid == 1 ||
+      count(module_implements('node_grants')) ||
+      !in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD')) ||
+      in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))
+    ) {
+      // Non-cached blocks get rendered immediately. Provides more content
+      // that can be easily manipulated during hook_page_alter().
+      $build[$key] = _block_render_block($build[$key]);
+    }
+    else {
+      $build[$key] += array(
+        '#pre_render' => array('_block_render_block'),
+        '#cache' => array(
+          'keys' => array($block->module, $block->delta),
+          'granularity' => $block->cache,
+          'bin' => 'cache_block',
+          'expire' => CACHE_TEMPORARY,
+        ),
+      );
+    }
 
     // Add contextual links for this block; skip the main content block, since
     // contextual links are basically output as tabs/local tasks already. Also
@@ -341,12 +373,6 @@ function _block_get_renderable_array($list = array()) {
     if ($key != 'system_main' && $key != 'system_help') {
       $build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($block->module, $block->delta));
     }
-
-    $build[$key] += array(
-      '#block' => $block,
-      '#weight' => ++$weight,
-    );
-    $build[$key]['#theme_wrappers'][] ='block';
   }
   $build['#sorted'] = TRUE;
   return $build;
@@ -653,9 +679,6 @@ function block_list($region) {
   if (!isset($blocks[$region])) {
     $blocks[$region] = array();
   }
-  else {
-    $blocks[$region] = _block_render_blocks($blocks[$region]);
-  }
 
   return $blocks[$region];
 }
@@ -695,6 +718,7 @@ function _block_load_blocks() {
   global $theme_key;
 
   $query = db_select('block', 'b');
+  $query->addField('b', 'title', 'subject');
   $result = $query
     ->fields('b')
     ->condition('b.theme', $theme_key)
@@ -801,96 +825,51 @@ function block_block_list_alter(&$blocks) {
 }
 
 /**
- * Render the content and subject for a set of blocks.
+ * Render the content and subject for a block.
  *
- * @param $region_blocks
- *   An array of block objects such as returned for one region by _block_load_blocks().
- *
- * @return
- *   An array of visible blocks as expected by drupal_render().
- */
-function _block_render_blocks($region_blocks) {
-  foreach ($region_blocks as $key => $block) {
-    // Render the block content if it has not been created already.
-    if (!isset($block->content)) {
-      // Erase the block from the static array - we'll put it back if it has
-      // content.
-      unset($region_blocks[$key]);
-      // Try fetching the block from cache. Block caching is not compatible
-      // with node_access modules. We also preserve the submission of forms in
-      // blocks, by fetching from cache only if the request method is 'GET'
-      // (or 'HEAD').
-      if (!count(module_implements('node_grants')) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
-        $array = $cache->data;
-      }
-      else {
-        $array = module_invoke($block->module, 'block_view', $block->delta);
+ * @param $element
+ *   A renderable array.
+ */
+function _block_render_block($element) {
+  $block = $element['#block'];
 
-        // Allow modules to modify the block before it is viewed, via either
-        // hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter().
-        drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
+  // Render the block content if it has not been created already.
+  if (!isset($block->content)) {
+    $array = module_invoke($block->module, 'block_view', $block->delta);
 
-        if (isset($cid)) {
-          cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
-        }
-      }
+    // Allow modules to modify the block before it is viewed, via either
+    // hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter().
+    drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
 
-      if (isset($array) && is_array($array)) {
-        foreach ($array as $k => $v) {
-          $block->$k = $v;
-        }
-      }
-      if (isset($block->content) && $block->content) {
-        // Normalize to the drupal_render() structure.
-        if (is_string($block->content)) {
-          $block->content = array('#markup' => $block->content);
-        }
-        // Override default block title if a custom display title is present.
-        if ($block->title) {
-          // Check plain here to allow module generated titles to keep any
-          // markup.
-          $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
-        }
-        if (!isset($block->subject)) {
-          $block->subject = '';
-        }
-        $region_blocks["{$block->module}_{$block->delta}"] = $block;
+    if (empty($array)) {
+      // Blocks without contents should emit no markup at all.
+      $element['#access'] = FALSE;
+    }
+    elseif (isset($array) && is_array($array)) {
+      foreach ($array as $k => $v) {
+        $block->$k = $v;
       }
     }
   }
-  return $region_blocks;
-}
 
-/**
- * Assemble the cache_id to use for a given block.
- *
- * The cache_id string reflects the viewing context for the current block
- * instance, obtained by concatenating the relevant context information
- * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_*
- * constants). Two block instances can use the same cached content when
- * they share the same cache_id.
- *
- * Theme and language contexts are automatically differentiated.
- *
- * @param $block
- * @return
- *   The string used as cache_id for the block.
- */
-function _block_get_cache_id($block) {
-  global $user;
-
-  // User 1 being out of the regular 'roles define permissions' schema,
-  // it brings too many chances of having unwanted output get in the cache
-  // and later be served to other users. We therefore exclude user 1 from
-  // block caching.
-  if (variable_get('block_cache', FALSE) && !in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM)) && $user->uid != 1) {
-    // Start with common sub-patterns: block identification, theme, language.
-    $cid_parts[] = $block->module;
-    $cid_parts[] = $block->delta;
-    $cid_parts = array_merge($cid_parts, drupal_render_cid_parts($block->cache));
-
-    return implode(':', $cid_parts);
+  if (isset($block->content) && $block->content) {
+    // Normalize to the drupal_render() structure.
+    if (is_string($block->content)) {
+      $block->content = array('#markup' => $block->content);
+    }
+    // Override default block title if a custom display title is present.
+    if ($block->title) {
+      // Check plain here to allow module generated titles to keep any
+      // markup.
+      $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
+    }
+
+    // Add the content renderable array to the main element.
+    $element['content'] = $block->content;
+    unset($block->content);
+    $element['#block'] = $block;
   }
+  return $element;
 }
 
 /**
@@ -975,21 +954,6 @@ function block_menu_delete($menu) {
 }
 
 /**
- * Implements hook_form_FORM_ID_alter().
- */
-function block_form_system_performance_settings_alter(&$form, &$form_state) {
-  $disabled = count(module_implements('node_grants'));
-  $form['caching']['block_cache'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Cache blocks'),
-    '#default_value' => variable_get('block_cache', FALSE),
-    '#disabled' => $disabled,
-    '#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
-    '#weight' => -1,
-  );
-}
-
-/**
  * Implements hook_admin_paths().
  */
 function block_admin_paths() {
diff --git a/modules/block/block.test b/modules/block/block.test
index af118a9..c2416e2 100644
--- a/modules/block/block.test
+++ b/modules/block/block.test
@@ -470,9 +470,6 @@ class BlockCacheTestCase extends DrupalWebTestCase {
     user_save($this->normal_user_alt, array('roles' => $this->normal_user->roles));
     $this->normal_user_alt->roles = $this->normal_user->roles;
 
-    // Enable block caching.
-    variable_set('block_cache', TRUE);
-
     // Enable our test block.
     $edit['blocks[block_test_test_cache][region]'] = 'sidebar_first';
     $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
diff --git a/modules/dashboard/dashboard.module b/modules/dashboard/dashboard.module
index 08a4cd2..ce6cf52 100644
--- a/modules/dashboard/dashboard.module
+++ b/modules/dashboard/dashboard.module
@@ -526,8 +526,7 @@ function dashboard_show_block_content($module, $delta) {
     ->fetchObject();
   $block_object->enabled = $block_object->page_match = TRUE;
   $blocks[$module . "_" . $delta] = $block_object;
-  $block_content = _block_render_blocks($blocks);
-  $build = _block_get_renderable_array($block_content);
+  $build = _block_get_renderable_array($blocks);
   $rendered_block = drupal_render($build);
   print $rendered_block;
   drupal_exit();
-- 
1.7.4.1

