diff --git a/modules/block/block.module b/modules/block/block.module index 3a988de..dde26ea 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -880,9 +880,11 @@ function _block_render_blocks($region_blocks) { else { $array = module_invoke($block->module, 'block_view', $block->delta); + // Valid PHP function names cannot contain hyphens. + $delta = str_replace('-', '_', $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); + drupal_alter(array('block_view', "block_view_{$block->module}_{$delta}"), $array, $block); if (isset($cid)) { cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY); diff --git a/modules/block/block.test b/modules/block/block.test index 11d0709..aad01f7 100644 --- a/modules/block/block.test +++ b/modules/block/block.test @@ -193,7 +193,7 @@ class BlockTestCase extends DrupalWebTestCase { } /** - * Test block visibility when using "pages" restriction but leaving + * Test block visibility when using "pages" restriction but leaving * "pages" textarea empty */ function testBlockVisibilityListedEmpty() { @@ -753,6 +753,47 @@ class BlockTemplateSuggestionsUnitTest extends DrupalUnitTestCase { } /** + * Unit tests for hook_block_view_MODULE_DELTA_alter(). + */ +class BlockViewModuleDeltaAlterWebTest extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Block view module delta alter', + 'description' => 'Test the hook_block_view_MODULE_DELTA_alter() hook.', + 'group' => 'Block', + ); + } + + public function setUp() { + parent::setUp(array('block_test')); + } + + /** + * Test if hook_block_view_MODULE_DELTA_alter() is called, even if the delta + * contains a hyphen. + */ + public function testBlockViewModuleDeltaAlter() { + $block = new stdClass; + $block->module = 'block_test'; + $block->delta = 'test_underscore'; + $block->title = ''; + $render = array_pop(_block_render_blocks(array('region' => $block))); + $test_underscore = $render->content['#markup']; + $this->assertEqual($test_underscore, 'hook_block_view_MODULE_DELTA_alter', 'Found expected altered block content for delta with underscore'); + + $block = new stdClass; + $block->module = 'block_test'; + $block->delta = 'test-hyphen'; + $block->title = ''; + $render = array_pop(_block_render_blocks(array('region' => $block))); + $test_hyphen = $render->content['#markup']; + $this->assertEqual($test_hyphen, 'hook_block_view_MODULE_DELTA_alter', 'Hyphens (-) in block delta were replaced by underscore (_)'); + } + +} + +/** * Tests that hidden regions do not inherit blocks when a theme is enabled. */ class BlockHiddenRegionTestCase extends DrupalWebTestCase { diff --git a/modules/block/tests/block_test.module b/modules/block/tests/block_test.module index 5e06d5c..475ebc8 100644 --- a/modules/block/tests/block_test.module +++ b/modules/block/tests/block_test.module @@ -22,6 +22,14 @@ function block_test_block_info() { 'cache' => variable_get('block_test_caching', DRUPAL_CACHE_PER_ROLE), ); + $blocks['test_underscore'] = array( + 'info' => t('Test underscore'), + ); + + $blocks['test-hyphen'] = array( + 'info' => t('Test hyphen'), + ); + $blocks['test_html_id'] = array( 'info' => t('Test block html id'), ); @@ -34,3 +42,17 @@ function block_test_block_info() { function block_test_block_view($delta = 0) { return array('content' => variable_get('block_test_content', '')); } + +/** + * Implements hook_block_view_MODULE_DELTA_alter(). + */ +function block_test_block_view_block_test_test_underscore_alter(&$data, $block) { + $data['content'] = 'hook_block_view_MODULE_DELTA_alter'; +} + +/** + * Implements hook_block_view_MODULE_DELTA_alter(). + */ +function block_test_block_view_block_test_test_hyphen_alter(&$data, $block) { + $data['content'] = 'hook_block_view_MODULE_DELTA_alter'; +}