diff --git a/core/modules/block/block.module b/core/modules/block/block.module index a95220d..8afc6de 100755 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -603,14 +603,13 @@ function template_preprocess_block(&$variables) { // contains a hyphen, it will end up as an underscore after this conversion, // and your function names won't be recognized. So, we need to convert // hyphens to underscores in block deltas for the theme suggestions. - if (!empty($variables['block']->derivative)) { - list($plugin, $derivative) = explode(':', $variables['block']->id); - $variables['theme_hook_suggestions'][] = 'block__' . strtr($plugin, '-', '_'); - $variables['theme_hook_suggestions'][] = 'block__' . strtr($plugin, '-', '_') . '__' . strtr($derivative, '-', '_'); - } - else { - $plugin = $variables['block']->id; - $variables['theme_hook_suggestions'][] = 'block__' . strtr($plugin, '-', '_'); + + // We can safely explode on : because we know the Block plugin type manager + // enforces that delimiter for all derivatives. + $parts = explode(':', $variables['elements']['#block']->getPluginId()); + $suggestion = 'block'; + while ($part = array_shift($parts)) { + $variables['theme_hook_suggestions'][] = $suggestion .= '__' . strtr($part, '-', '_'); } // Create a valid HTML ID and make sure it is unique. if (!empty($variables['block']->config_id)) { diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php index 3b5b389..4078132 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php @@ -7,13 +7,20 @@ namespace Drupal\block\Tests; -use Drupal\simpletest\UnitTestBase; -use stdClass; +use Drupal\simpletest\WebTestBase; /** * Unit tests for template_preprocess_block(). */ -class BlockTemplateSuggestionsUnitTest extends UnitTestBase { +class BlockTemplateSuggestionsUnitTest extends WebTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('block'); + public static function getInfo() { return array( 'name' => 'Block template suggestions', @@ -26,31 +33,23 @@ public static function getInfo() { * Test if template_preprocess_block() handles the suggestions right. */ function testBlockThemeHookSuggestions() { - // Define block delta with underscore to be preprocessed - $block1 = new stdClass(); - $block1->module = 'block'; - $block1->delta = 'underscore_test'; - $block1->region = 'footer'; - $variables1 = array(); - $variables1['elements']['#block'] = $block1; - $variables1['elements']['#children'] = ''; - template_preprocess_block($variables1); - $this->assertEqual($variables1['theme_hook_suggestions'], array('block__footer', 'block__block', 'block__block__underscore_test'), 'Found expected block suggestions for delta with underscore'); - - // Define block delta with hyphens to be preprocessed. Hyphens should be - // replaced with underscores. - $block2 = new stdClass(); - $block2->module = 'block'; - $block2->delta = 'hyphen-test'; - $block2->region = 'footer'; - $variables2 = array(); - $variables2['elements']['#block'] = $block2; - $variables2['elements']['#children'] = ''; + // Define a block with a derivative to be preprocessed, which includes both + // an underscore (not transformed) and a hyphen (transformed to underscore), + // and generates possibilities for each level of derivative. + + $data = array( + 'region' => 'footer', + ); + + // $block3 = block_load('system_powered_by_block', $data3); + $block = block_manager()->createInstance('system_menu_block:menu-admin', $data); + $variables = array(); + $variables['elements']['#block'] = $block; + $variables['elements']['#children'] = ''; // Test adding a class to the block content. - $variables2['content_attributes']['class'][] = 'test-class'; - template_preprocess_block($variables2); - $this->assertEqual($variables2['theme_hook_suggestions'], array('block__footer', 'block__block', 'block__block__hyphen_test'), 'Hyphens (-) in block delta were replaced by underscore (_)'); - // Test that the default class and added class are available. - $this->assertEqual($variables2['content_attributes']['class'], array('test-class', 'content'), 'Default .content class added to block content_attributes_array'); + $variables['content_attributes']['class'][] = 'test-class'; + template_preprocess_block($variables); + $this->assertEqual($variables['theme_hook_suggestions'], array('block__footer', 'block__system', 'block__system_menu_block', 'block__system_menu_block__menu_admin')); + $this->assertEqual($variables['content_attributes']['class'], array('test-class', 'content'), 'Default .content class added to block content_attributes_array'); } } diff --git a/core/modules/menu/lib/Drupal/menu/Plugin/Derivative/MenuBlock.php b/core/modules/menu/lib/Drupal/menu/Plugin/Derivative/MenuBlock.php index 04a4c34..496a268 100644 --- a/core/modules/menu/lib/Drupal/menu/Plugin/Derivative/MenuBlock.php +++ b/core/modules/menu/lib/Drupal/menu/Plugin/Derivative/MenuBlock.php @@ -16,7 +16,6 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { } foreach (menu_get_menus(FALSE) as $menu => $name) { $this->derivatives[$menu] = $base_plugin_definition; - $this->derivatives[$menu]['delta'] = $menu; $this->derivatives[$menu]['subject'] = t('Menu: @menu', array('@menu' => $name)); $this->derivatives[$menu]['cache'] = DRUPAL_NO_CACHE; }