From 1804d9b77744d5531e29d7d5938359fe5df0caa8 Mon Sep 17 00:00:00 2001
From: Chris Johnson <drupal@tekante.com>
Date: Mon, 28 Feb 2011 07:55:17 -0500
Subject: [PATCH] Issue 996426 - Added code to ensure blocks are never empty if the context_ui inline editor is going to appear on the page to resolve a bug where empty blocks are dropped from saved contexts

---
 context.module                     |   12 ++++++++++++
 plugins/context_reaction_block.inc |   18 ++++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/context.module b/context.module
index 78f3f51..ea1ef30 100644
--- a/context.module
+++ b/context.module
@@ -506,3 +506,15 @@ function _context_registry($key = NULL, $reset = FALSE) {
   }
   return $registry;
 }
+
+/**
+ * hook_block_view_alter - if the context editor block is on this page,
+ * ensure that all blocks have some content so that empty blocks are
+ * not dropped
+ */
+function context_block_view_alter(&$data, $block) {
+  $context_ui_editor_present = &drupal_static('context_ui_editor_present', FALSE);
+  if ($context_ui_editor_present && empty($data['content'])) {
+    $data['content']['#markup'] = "<div class='context-block-empty'>". t('This block appears empty when displayed on this page.') . "</div>";
+  }
+}
diff --git a/plugins/context_reaction_block.inc b/plugins/context_reaction_block.inc
index e15e5a4..fc576ff 100644
--- a/plugins/context_reaction_block.inc
+++ b/plugins/context_reaction_block.inc
@@ -334,6 +334,24 @@ class context_reaction_block extends context_reaction {
           }
         }
       }
+
+      $context_ui_editor_present = &drupal_static('context_ui_editor_present', FALSE);
+      foreach ($context_blocks as $r => $blocks) {
+        if (isset($blocks['context_ui-editor'])) {
+          $block = $blocks['context_ui-editor'];
+          // see if the editor is actually enabled, lifted from _block_render_blocks
+          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);
+
+            drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
+          }
+          $context_ui_editor_present = !empty($array['content']);
+          break;
+        }
+      }
       foreach ($context_blocks as $r => $blocks) {
         $context_blocks[$r] = _block_render_blocks($blocks);
 
-- 
1.5.5.6

