diff --git a/core/modules/block/block.api.php b/core/modules/block/block.api.php index c9b3b1f..f1775dc 100644 --- a/core/modules/block/block.api.php +++ b/core/modules/block/block.api.php @@ -60,7 +60,7 @@ function hook_block_view_alter(array &$build, \Drupal\block\Plugin\Core\Entity\B * @see hook_block_view_alter() * @see hook_block_view_NAME_alter() */ -function hook_block_view_ID_alter(array &$build, \Drupal\block\BlockInterface $block) { +function hook_block_view_ID_alter(array &$build, \Drupal\block\Plugin\Core\Entity\Block $block) { // This code will only run for a specific block. For example, if ID // in the function definition above is set to "someid", the code // will only run on the "someid" block. @@ -89,7 +89,7 @@ function hook_block_view_ID_alter(array &$build, \Drupal\block\BlockInterface $b * @see hook_block_view_alter() * @see hook_block_view_ID_alter() */ -function hook_block_view_NAME_alter(array &$build, \Drupal\block\BlockInterface $block) { +function hook_block_view_NAME_alter(array &$build, \Drupal\block\Plugin\Core\Entity\Block $block) { // This code will only run for a specific block instance. For example, if NAME // in the function definition above is set to "someid", the code will only run // on the "someid" block. diff --git a/core/modules/block/lib/Drupal/block/BlockRenderController.php b/core/modules/block/lib/Drupal/block/BlockRenderController.php index e1c560e..d64b385 100644 --- a/core/modules/block/lib/Drupal/block/BlockRenderController.php +++ b/core/modules/block/lib/Drupal/block/BlockRenderController.php @@ -78,9 +78,13 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la } $build[$entity_id]['content'] = $content; - // All blocks, even when empty, should be available for altering. + // Valid PHP function names cannot contain hyphens. $id = str_replace(':', '__', $entity->get('plugin')); - list(, $name) = $entity->id(); + $id = str_replace('-', '_', $id); + list(, $name) = explode('.', $entity->id()); + $name = str_replace('-', '_', $name); + + // All blocks, even when empty, should be available for altering. drupal_alter(array('block_view', "block_view_$id", "block_view_$name"), $build[$entity_id], $entity); } diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockViewAlterTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockViewAlterTest.php new file mode 100644 index 0000000..3a51645 --- /dev/null +++ b/core/modules/block/lib/Drupal/block/Tests/BlockViewAlterTest.php @@ -0,0 +1,51 @@ + 'Block view alter hook', + 'description' => 'Test the hook block view alter hooks.', + 'group' => 'Block', + ); + } + + function setUp() { + parent::setUp(); + + $this->drupalLogin($this->root_user); + } + + /** + * Tests if the view alter function is called when the block ID or machine + * name contains a hyphen. + */ + function testHyphen() { + // Enable our test blocks. + $this->drupalPlaceBlock('test-id-hyphen', array('machine_name' => 'test-name-hyphen')); + + $this->drupalGet(''); + $this->assertText('hook_block_view_ID_alter() invoked.'); + $this->assertText('hook_block_view_NAME_alter() invoked.'); + } + +} diff --git a/core/modules/block/tests/block_test.module b/core/modules/block/tests/block_test.module index 6abb95f..4885e12 100644 --- a/core/modules/block/tests/block_test.module +++ b/core/modules/block/tests/block_test.module @@ -12,3 +12,17 @@ function block_test_system_theme_info() { $themes['block_test_theme'] = drupal_get_path('module', 'block_test') . '/themes/block_test_theme/block_test_theme.info.yml'; return $themes; } + +/** + * Implements hook_block_view_ID_alter(). + */ +function block_test_block_view_test_id_hyphen_alter(array &$build, Drupal\block\Plugin\Core\Entity\Block $block) { + $build['content']['#children'] .= 'hook_block_view_ID_alter() invoked.'; +} + +/** + * Implements hook_block_view_NAME_alter(). + */ +function block_test_block_view_test_name_hyphen_alter(array &$build, Drupal\block\Plugin\Core\Entity\Block $block) { + $build['content']['#children'] .= 'hook_block_view_NAME_alter() invoked.'; +} diff --git a/core/modules/block/tests/lib/Drupal/block_test/Plugin/block/block/TestViewAlterBlock.php b/core/modules/block/tests/lib/Drupal/block_test/Plugin/block/block/TestViewAlterBlock.php new file mode 100644 index 0000000..0b14489 --- /dev/null +++ b/core/modules/block/tests/lib/Drupal/block_test/Plugin/block/block/TestViewAlterBlock.php @@ -0,0 +1,34 @@ + state()->get('block_test.content'), + ); + } + +}