Patch #512464 by Damien Tournoud: fix automatic block enabling, enable system blocks by standard mechanisms.

From: damz <damz@damz-dev.local>


---
 block/block.admin.inc   |   14 +++++++--
 block/block.module      |   28 ++++++++++++++-----
 block/block.test        |    5 ++-
 system/system.module    |   12 ++++++++
 user/user.module        |   21 +++++++++-----
 default/default.profile |   70 -----------------------------------------------
 expert/expert.profile   |   58 ---------------------------------------
 7 files changed, 60 insertions(+), 148 deletions(-)

diff --git modules/block/block.admin.inc modules/block/block.admin.inc
index 0fd1742..9d78d49 100644
--- modules/block/block.admin.inc
+++ modules/block/block.admin.inc
@@ -16,7 +16,9 @@ function block_admin_display($theme = NULL) {
   $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
 
   // Fetch and sort blocks.
-  $blocks = _block_rehash();
+  $blocks = block_rehash($custom_theme);
+  // Pass information about the theme to _block_compare().
+  _block_compare(NULL, NULL, $custom_theme);
   usort($blocks, '_block_compare');
 
   return drupal_get_form('block_admin_display_form', $blocks, $theme);
@@ -124,9 +126,13 @@ function block_admin_display_form_submit($form, &$form_state) {
  * Active blocks are sorted by region, then by weight.
  * Disabled blocks are sorted by name.
  */
-function _block_compare($a, $b) {
-  global $theme_key;
-  $regions = &drupal_static(__FUNCTION__);
+function _block_compare($a = NULL, $b = NULL, $theme = NULL) {
+  static $theme_key, $regions;
+  if (isset($theme)) {
+    $theme_key = $theme;
+    $regions = NULL
+    return;
+  }
 
   // We need the region list to correctly order by region.
   if (!isset($regions)) {
diff --git modules/block/block.module modules/block/block.module
index a50bc72..2c80f8f 100644
--- modules/block/block.module
+++ modules/block/block.module
@@ -181,6 +181,14 @@ function _block_themes_access($theme) {
 }
 
 /**
+ * Implement hook_modules_enabled().
+ */
+function block_modules_enabled() {
+  // Rehash the blocks.
+  block_rehash_all();
+}
+
+/**
  * Implement hook_block_list().
  */
 function block_block_list() {
@@ -285,16 +293,22 @@ function block_get_blocks_by_region($region) {
 }
 
 /**
- * Update the 'block' DB table with the blocks currently exported by modules.
+ * Update the {block} table with the blocks currently exported by modules for all themes.
+ */
+function block_rehash_all() {
+  $themes = db_query("SELECT name FROM {system} WHERE type = :type AND status = :status", array(':type' => 'theme', ':status' => 1));
+  foreach ($themes as $theme) {
+    block_rehash($theme->name);
+  }
+}
+
+/**
+ * Update the {block} table with the blocks currently exported by modules.
  *
  * @return
  *   Blocks currently exported by modules.
  */
-function _block_rehash() {
-  global $theme_key;
-
-  drupal_theme_initialize();
-
+function block_rehash($theme_key) {
   $old_blocks = array();
   $result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme_key));
   foreach ($result as $old_block) {
@@ -544,7 +558,7 @@ function block_theme_initialize($theme) {
     $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'custom', 'cache'));
     foreach ($result as $block) {
       // If the region isn't supported by the theme, assign the block to the theme's default region.
-      if (!array_key_exists($block['region'], $regions)) {
+      if (!empty($block['region']) && !array_key_exists($block['region'], $regions)) {
         $block['region'] = system_default_region($theme);
       }
       $block['theme'] = $theme;
diff --git modules/block/block.test modules/block/block.test
index 065c1c0..36ed432 100644
--- modules/block/block.test
+++ modules/block/block.test
@@ -259,10 +259,11 @@ class NewDefaultThemeBlocks extends DrupalWebTestCase {
     // Turn on the Stark theme and ensure that it contains all of the blocks
     // that Garland did.
     $this->drupalPost('admin/build/themes', array('theme_default' => 'stark'), t('Save configuration'));
-    $result = db_query("SELECT * FROM {block} WHERE theme='stark'");
+
+    $result = db_query("SELECT * FROM {block} WHERE theme = 'stark'");
     foreach ($result as $block) {
       unset($block->theme, $block->bid);
-      $this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block matched'));
+      $this->assertEqual($blocks[$block->module][$block->delta], $block, t('Block matched: %module-%delta', array('%module' => $block->module, '%delta' => $block->delta)));
     }
   }
 }
diff --git modules/system/system.module modules/system/system.module
index fa0f5c7..a90bb13 100644
--- modules/system/system.module
+++ modules/system/system.module
@@ -1426,15 +1426,21 @@ function system_block_list() {
     'info' => t('Main page content'),
      // Cached elsewhere.
     'cache' => BLOCK_NO_CACHE,
+    'status' => 1,
+    'region' => 'content',
   );
   $blocks['powered-by'] = array(
     'info' => t('Powered by Drupal'),
     'weight' => '10',
     'cache' => BLOCK_NO_CACHE,
+    'status' => 1,
+    'region' => 'footer',
   );
   $blocks['help'] = array(
     'info' => t('System help'),
     'weight' => '5',
+    'status' => 1,
+    'region' => 'help',
   );
   // System-defined menu blocks.
   foreach (menu_list_system_menus() as $menu_name => $title) {
@@ -1442,6 +1448,12 @@ function system_block_list() {
     // Menu blocks can't be cached because each menu item can have
     // a custom access callback. menu.inc manages its own caching.
     $blocks[$menu_name]['cache'] = BLOCK_NO_CACHE;
+
+    // Enable the 'navigation' and 'management' blocks by default.
+    if (in_array($menu_name, array('navigation', 'management'))) {
+      $blocks[$menu_name]['status'] = 1;
+      $blocks[$menu_name]['region'] = 'left';
+    }
   }
   return $blocks;
 }
diff --git modules/user/user.module modules/user/user.module
index 3749e1f..229fa05 100644
--- modules/user/user.module
+++ modules/user/user.module
@@ -1020,15 +1020,22 @@ function user_login_block() {
 function user_block_list() {
   global $user;
 
-  $blocks['login']['info'] = t('User login');
-  // Not worth caching.
-  $blocks['login']['cache'] = BLOCK_NO_CACHE;
+  $blocks['login'] = array(
+    'info' => t('User login'),
+    // Not worth caching.
+    'cache' => BLOCK_NO_CACHE,
+    'status' => 1,
+    'region' => 'left',
+  );
 
-  $blocks['new']['info'] = t('Who\'s new');
+  $blocks['new'] = array(
+    'info' => t("Who's new"),
+  );
 
-  // Too dynamic to cache.
-  $blocks['online']['info'] = t('Who\'s online');
-  $blocks['online']['cache'] = BLOCK_NO_CACHE;
+  $blocks['online'] = array(
+    'info' => t("Who's online"),
+    'cache' => BLOCK_NO_CACHE,
+  );
   return $blocks;
 }
 
diff --git profiles/default/default.profile profiles/default/default.profile
index 81f8cc1..4a73ccd 100644
--- profiles/default/default.profile
+++ profiles/default/default.profile
@@ -90,76 +90,6 @@ function default_profile_task_list() {
  *   modify the $task, otherwise discarded.
  */
 function default_profile_tasks(&$task, $url) {
-  
-  // Enable some standard blocks.
-  $values = array(
-    array(
-      'module' => 'system',
-      'delta' => 'main',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'content',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'user',
-      'delta' => 'login',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'navigation',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'management',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 1,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'powered-by',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 10,
-      'region' => 'footer',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'help',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'help',
-      'pages' => '',
-      'cache' => -1,
-    ),
-  );
-  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
-  foreach ($values as $record) {
-    $query->values($record);
-  }
-  $query->execute();  
-
   // Insert default user-defined node types into the database. For a complete
   // list of available node type attributes, refer to the node type API
   // documentation at: http://api.drupal.org/api/HEAD/function/hook_node_info.
diff --git profiles/expert/expert.profile profiles/expert/expert.profile
index 641b280..a3211ad 100644
--- profiles/expert/expert.profile
+++ profiles/expert/expert.profile
@@ -43,64 +43,6 @@ function expert_profile_task_list() {
  */
 function expert_profile_tasks(&$task, $url) {
 
-  // Enable some standard blocks.
-  $values = array(
-    array(
-      'module' => 'system',
-      'delta' => 'main',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'content',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'user',
-      'delta' => 'login',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'navigation',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'management',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 1,
-      'region' => 'left',
-      'pages' => '',
-      'cache' => -1,
-    ),
-    array(
-      'module' => 'system',
-      'delta' => 'help',
-      'theme' => 'garland',
-      'status' => 1,
-      'weight' => 0,
-      'region' => 'help',
-      'pages' => '',
-      'cache' => -1,
-    ),
-  );
-  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
-  foreach ($values as $record) {
-    $query->values($record);
-  }
-  $query->execute();  
 }
 
 /**
