From 226fe6997057f1b2b58116e7bc35ffbcbaed726c Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Mon, 30 Dec 2013 11:45:54 -0500 Subject: Issue #1076132 by fizk, friesk, foxtrotcharlie, coolestdude1, David_Rothstein, skwashd, alexpott, tstoeckler | adaddinsane: Hook_block_view_MODULE_DELTA_alter fails with blocks that have a hyphen in the block delta. --- modules/block/block.module | 4 +++- modules/block/block.test | 42 +++++++++++++++++++++++++++++++++++ modules/block/tests/block_test.module | 22 ++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) (limited to 'modules/block') diff --git a/modules/block/block.module b/modules/block/block.module index 3a988de2c..dde26ea8e 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 11d070993..c1afec4bf 100644 --- a/modules/block/block.test +++ b/modules/block/block.test @@ -752,6 +752,48 @@ class BlockTemplateSuggestionsUnitTest extends DrupalUnitTestCase { } } +/** + * 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')); + } + + /** + * Tests that the alter hook 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 = _block_render_blocks(array('region' => $block)); + $render = array_pop($render_array); + $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 = _block_render_blocks(array('region' => $block)); + $render = array_pop($render_array); + $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. */ diff --git a/modules/block/tests/block_test.module b/modules/block/tests/block_test.module index 5e06d5cf5..475ebc895 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'; +} -- cgit v1.2.3