#918808: delay the rendering of blocks to enable smart cache strategies.

From: Damien Tournoud <damien@commerceguys.com>


---
 block/block.module         |  114 +++++++++++++++++++++-----------------------
 dashboard/dashboard.module |    3 -
 2 files changed, 55 insertions(+), 62 deletions(-)

diff --git modules/block/block.module modules/block/block.module
index f701482..f3d73d6 100644
--- modules/block/block.module
+++ modules/block/block.module
@@ -333,8 +333,11 @@ 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(
+      '#pre_render' => array('_block_pre_render_block'),
+      '#block' => $block,
+      '#weight' => ++$weight,
+    );
 
     // Add contextual links for this block; skip the main content block, since
     // contextual links are basically output as tabs/local tasks already. Also
@@ -344,12 +347,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;
@@ -647,9 +644,6 @@ function block_list($region) {
   if (!isset($blocks[$region])) {
     $blocks[$region] = array();
   }
-  else {
-    $blocks[$region] = _block_render_blocks($blocks[$region]);
-  }
 
   return $blocks[$region];
 }
@@ -795,64 +789,64 @@ function block_block_list_alter(&$blocks) {
 }
 
 /**
- * Render the content and subject for a set of blocks.
+ * Pre-render fucntion: 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_pre_render_block($element) {
+  $block = $element['#block'];
+
+  // Render the block content if it has not been created already.
+  if (!isset($block->content)) {
+    // 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);
 
-        // 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);
+      // 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($cid)) {
-          cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
-        }
+      if (isset($cid)) {
+        cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
       }
+    }
 
-      if (isset($array) && is_array($array)) {
-        foreach ($array as $k => $v) {
-          $block->$k = $v;
-        }
+    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 (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 = '';
+      }
+
+      // Replace the stub block element with the full renderable array
+      // that we have now generated.
+      $element = $block->content;
+      unset($block->content);
+      $element['#block'] = $block;
+      $element['#theme_wrappers'][] = 'block';
     }
   }
-  return $region_blocks;
+  return $element;
 }
 
 /**
diff --git modules/dashboard/dashboard.module modules/dashboard/dashboard.module
index fad6a9e..54c3b41 100644
--- modules/dashboard/dashboard.module
+++ modules/dashboard/dashboard.module
@@ -490,8 +490,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();
