diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f8358eb..19e3bab 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1069,11 +1069,9 @@ function theme($hook, $variables = array()) { } // Add preprocess and process functions from the base hook to the $info // array. - // @todo Should this be array_merge() or NestedArray::mergeDeep() or handled - // in a different way altogether? foreach (array('preprocess functions', 'process functions') as $processor) { if (isset($base_hook_info[$processor])) { - $info += array($processor => $base_hook_info[$processor]); + $info = array_merge($info, array($processor => $base_hook_info[$processor])); } } } @@ -2856,9 +2854,8 @@ function template_process_html(&$variables) { * * @return * An array of suggestions, suitable for adding to - * hook_theme_suggestions_HOOK or to - * $variables['attributes']['class'] if the suggestions represent extra CSS - * classes. + * hook_theme_suggestions_HOOK_alter() or to $variables['attributes']['class'] + * if the suggestions represent extra CSS classes. */ function theme_get_suggestions($args, $base, $delimiter = '__') { diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php index 51630ba..bc4ab59 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php @@ -10,7 +10,7 @@ use Drupal\simpletest\WebTestBase; /** - * Unit tests for template_preprocess_block(). + * Unit tests for block_theme_suggestions_block_alter(). */ class BlockTemplateSuggestionsUnitTest extends WebTestBase { @@ -24,13 +24,13 @@ class BlockTemplateSuggestionsUnitTest extends WebTestBase { public static function getInfo() { return array( 'name' => 'Block template suggestions', - 'description' => 'Test the template_preprocess_block() function.', + 'description' => 'Test the block_theme_suggestions_block_alter() function.', 'group' => 'Block', ); } /** - * Test if template_preprocess_block() handles the suggestions right. + * Tests template suggestions from block_theme_suggestions_block_alter(). */ function testBlockThemeHookSuggestions() { // Define a block with a derivative to be preprocessed, which includes both @@ -43,16 +43,15 @@ function testBlockThemeHookSuggestions() { 'id' => config('system.theme')->get('default') . '.machinename', )); + // @todo Cast to array in suggestions alter hook signature? + $suggestions = array(); $variables = array(); $variables['elements']['#block'] = $block; $variables['elements']['#configuration'] = $block->getPlugin()->getConfig(); $variables['elements']['#plugin_id'] = $block->get('plugin'); $variables['elements']['content'] = array(); - // Test adding a class to the block content. - $variables['content_attributes']['class'][] = 'test-class'; - template_preprocess_block($variables); - $this->assertEqual($variables['theme_hook_suggestions'], array('block__system', 'block__system_menu_block', 'block__system_menu_block__menu_admin', 'block__machinename')); - $this->assertEqual($variables['content_attributes']['class'], array('test-class', 'content'), 'Default .content class added to block content_attributes'); + block_theme_suggestions_block_alter($suggestions, $variables); + $this->assertEqual($suggestions, array('block__system', 'block__system_menu_block', 'block__system_menu_block__menu_admin', 'block__machinename')); } } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 8fc3b0f..c20f9cf 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -957,12 +957,12 @@ function field_page_build(&$page) { function field_theme_suggestions_field_alter(&$suggestions, $variables) { $element = $variables['element']; - $suggestions += array( + $suggestions = array_merge($suggestions, array( 'field__' . $element['#field_type'], 'field__' . $element['#field_name'], 'field__' . $element['#bundle'], 'field__' . $element['#field_name'] . '__' . $element['#bundle'], - ); + )); } /** diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 9426708..71a1435 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1025,10 +1025,10 @@ function node_preprocess_block(&$variables) { function node_theme_suggestions_node_alter(&$suggestions, $variables) { $node = $variables['elements']['#node']; - $suggestions += array( + $suggestions = array_merge($suggestions, array( 'node__' . $node->type, 'node__' . $node->nid, - ); + )); } /** diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 2b7b2a0..f004902 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1049,14 +1049,14 @@ function system_menu() { * Implements hook_theme_suggestions_HOOK_alter(). */ function system_theme_suggestions_html_alter(&$suggestions, $variables) { - $suggestions += theme_get_suggestions(arg(), 'html'); + $suggestions = array_merge($suggestions, theme_get_suggestions(arg(), 'html')); } /** * Implements hook_theme_suggestions_HOOK_alter(). */ function system_theme_suggestions_page_alter(&$suggestions, $variables) { - $suggestions += theme_get_suggestions(arg(), 'page'); + $suggestions = array_merge($suggestions, theme_get_suggestions(arg(), 'page')); } /** diff --git a/core/modules/views/views.module b/core/modules/views/views.module index c7077e9..e0da079 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -238,17 +238,13 @@ function views_plugin_list() { function views_preprocess_node(&$vars) { Drupal::moduleHandler()->loadInclude('node', 'views.inc'); // The 'view' attribute of the node is added in views_preprocess_node() + // @todo The comment above doesn't make any sense to me - it's saying the + // attribute is added in *this* function? if (!empty($vars['node']->view) && $vars['node']->view->storage->id()) { - $vars['view'] = $vars['node']->view; - $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage->id(); - if (!empty($vars['node']->view->current_display)) { - $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage->id() . '__' . $vars['node']->view->current_display; - - // If a node is being rendered in a view, and the view does not have a path, - // prevent drupal from accidentally setting the $page variable: - if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler->hasPath()) { - $vars['page'] = FALSE; - } + // If a node is being rendered in a view, and the view does not have a path, + // prevent drupal from accidentally setting the $page variable: + if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler->hasPath()) { + $vars['page'] = FALSE; } } @@ -259,16 +255,40 @@ function views_preprocess_node(&$vars) { } /** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function views_theme_suggestions_node_alter(&$suggestions, $variables) { + if (!empty($variables['node']->view) && $variables['node']->view->storage->id()) { + $variables['view'] = $variables['node']->view; + $suggestions[] = 'node__view__' . $variables['node']->view->storage->id(); + if (!empty($variables['node']->view->current_display)) { + $suggestions[] = 'node__view__' . $variables['node']->view->storage->id() . '__' . $variables['node']->view->current_display; + } + } +} + +/** * A theme preprocess function to automatically allow view-based node * templates if called from a view. */ function views_preprocess_comment(&$vars) { // The 'view' attribute of the node is added in template_preprocess_views_view_row_comment() + // @todo I can't find the function mentioned in the above comment. if (!empty($vars['comment']->view) && $vars['comment']->view->storage->id()) { $vars['view'] = &$vars['comment']->view; - $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['comment']->view->storage->id(); - if (!empty($vars['node']->view->current_display)) { - $vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['comment']->view->storage->id() . '__' . $vars['comment']->view->current_display; + } +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function views_theme_suggestions_comment_alter(&$suggestions, $variables) { + if (!empty($variables['comment']->view) && $variables['comment']->view->storage->id()) { + $suggestions[] = 'comment__view__' . $variables['comment']->view->storage->id(); + // @todo Shouldn't the below be checking $variables['comment'] instead of + // $variables['node']? + if (!empty($variables['node']->view->current_display)) { + $suggestions[] = 'comment__view__' . $variables['comment']->view->storage->id() . '__' . $variables['comment']->view->current_display; } } } diff --git a/core/modules/views_ui/views_ui.theme.inc b/core/modules/views_ui/views_ui.theme.inc index 8e503fc..48eb6d1 100644 --- a/core/modules/views_ui/views_ui.theme.inc +++ b/core/modules/views_ui/views_ui.theme.inc @@ -450,7 +450,13 @@ function template_preprocess_views_ui_view_preview_section(&$vars) { ); $vars['links'] = drupal_render($build); } - $vars['theme_hook_suggestions'][] = 'views_ui_view_preview_section__' . $vars['section']; +} + +/** + * Implements hook_theme_suggestions_HOOK_alter(). + */ +function views_ui_theme_suggestions_views_ui_view_preview_section_alter(&$suggestions, $variables) { + $suggestions[] = 'views_ui_view_preview_section__' . $variables['section']; } /**