diff --git a/modules/block/block.module b/modules/block/block.module index 9ebbcc5..182427c 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -407,15 +407,10 @@ function _block_rehash($theme = NULL) { ->condition('theme', $theme) ->execute(); $original_database_blocks = array(); - $alter_implemented = (bool) module_implements('block_info_alter'); foreach ($database_blocks as $block) { $module = $block['module']; $delta = $block['delta']; - // The copy is needed later only if the alter hook implemented, otherwise - // it is possible to keep track of changes inside this function. - if ($alter_implemented) { - $original_database_blocks[$module][$delta] = $block; - } + $original_database_blocks[$module][$delta] = $block; // The cache mode can only by set from hook_block_info(), so that has // precedence over the database's value. $cache = $block['cache']; @@ -456,6 +451,11 @@ function _block_rehash($theme = NULL) { // Set region to none if not enabled and make sure status is set. if (empty($block['status'])) { $block['status'] = 0; + // If the region is changing, this change needs to be recorded in the + // database. + if (!isset($block['region']) || $block['region'] != BLOCK_REGION_NONE) { + $changed_blocks[$module][$delta] = TRUE; + } $block['region'] = BLOCK_REGION_NONE; } // There is no point saving disabled blocks. Still, we need to save them @@ -475,7 +475,7 @@ function _block_rehash($theme = NULL) { // then save it. To determine whether there was a change during alter, // it is enough to examine the values for the keys in the original // database record as that contained every database field. - if (!$primary_keys || !empty($changed_blocks[$module][$delta]) || ($alter_implemented && array_diff_assoc($original_database_blocks[$module][$delta], $block))) { + if (!$primary_keys || !empty($changed_blocks[$module][$delta]) || array_diff_assoc($original_database_blocks[$module][$delta], $block)) { drupal_write_record('block', $block, $primary_keys); // Make it possible to test this. $block['saved'] = TRUE; diff --git a/modules/block/block.test b/modules/block/block.test index 45c63d8..cffe848 100644 --- a/modules/block/block.test +++ b/modules/block/block.test @@ -858,6 +858,9 @@ class BlockInvalidRegionTestCase extends DrupalWebTestCase { } } +/** + * Tests that block rehashing works correctly. + */ class BlockHashTestCase extends DrupalWebTestCase { public static function getInfo() { return array( @@ -871,6 +874,9 @@ class BlockHashTestCase extends DrupalWebTestCase { parent::setUp(array('block')); } + /** + * Tests that block rehashing does not write to the database too often. + */ function testBlockRehash() { // No hook_block_info_alter(), no save. $this->doRehash(); @@ -887,8 +893,20 @@ class BlockHashTestCase extends DrupalWebTestCase { $GLOBALS['conf']['block_test_info_alter'] = 1; $this->doRehash(TRUE); $this->assertWeight(10000); + // Now hook_block_info_alter() exists but already changed the block's + // weight before, so it should not be saved again. + $this->doRehash(); + $this->assertWeight(10000); } + /** + * Performs a block rehash and checks several related assertions. + * + * @param $alter_active + * Set to TRUE if the block_test module's hook_block_info_alter() + * implementation is expected to make a change that results in an existing + * block needing to be resaved to the database. Defaults to FALSE. + */ function doRehash($alter_active = FALSE) { $saves = 0; foreach (_block_rehash() as $block) { @@ -905,6 +923,12 @@ class BlockHashTestCase extends DrupalWebTestCase { $this->assertEqual($alter_active, $saves); } + /** + * Asserts that the block_test module's block has a given weight. + * + * @param $weight + * The expected weight. + */ function assertWeight($weight) { $db_weight = db_query('SELECT weight FROM {block} WHERE module = :module AND delta = :delta', array(':module' => 'block_test', ':delta' => 'test_html_id'))->fetchField(); // By casting to string the assert fails on FALSE.