From abc12f0862d7140eda87ae62cc90773fb224a685 Mon Sep 17 00:00:00 2001
From: Fabian Franz <github@fabian-franz.de>
Date: Sun, 12 Jun 2011 21:49:31 +0200
Subject: [PATCH] Issue #1173012 by carlos8f, Fabianx: Blocks disappear after core drupal 6.22 update

---
 modules/block/block.module |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/modules/block/block.module b/modules/block/block.module
index 9eecda7..5142851 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -61,6 +61,17 @@ define('BLOCK_CACHE_PER_PAGE', 0x0004);
 define('BLOCK_CACHE_GLOBAL', 0x0008);
 
 /**
+ * Implementation of hook_init().
+ */
+function block_init() {
+
+  // If the cache was cleared, rebuild the block list
+  if (!cache_get('block:blocks_hashed')) {
+    _block_rehash_blocks();
+  }
+}
+
+/**
  * Implementation of hook_help().
  */
 function block_help($path, $arg) {
@@ -301,17 +312,24 @@ function _block_rehash($theme = NULL) {
 
   // Remove blocks that are no longer defined by the code from the database.
   foreach ($old_blocks as $module => $old_module_blocks) {
-    foreach ($old_module_blocks as $delta => $block) {
-      db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme);
+    // This cleanup does not apply to disabled modules, to avoid configuration
+    // being lost when modules are disabled.
+    if (module_exists($module)) {
+      foreach ($old_module_blocks as $delta => $block) {
+        db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme);
+      }
     }
   }
+
+  cache_set('block:blocks_hashed', TRUE);
+
   return $blocks;
 }
 
 /**
- * Implementation of hook_flush_caches().
+ * Rehashes all blocks for the active themes.
  */
-function block_flush_caches() {
+function _block_rehash_blocks() {
   // Rehash blocks for active themes. We don't use list_themes() here,
   // because if MAINTENANCE_MODE is defined it skips reading the database,
   // and we can't tell which themes are active.
-- 
1.7.4.1

