diff -u b/core/includes/theme.inc b/core/includes/theme.inc --- b/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -565,9 +565,15 @@ } } if (isset($info['preprocess functions'])) { + $context = array( + 'hook' => $hook, + 'suggestions' => $suggestions, + 'suggestion' => $suggestion, + 'info' => $info, + ); foreach ($info['preprocess functions'] as $preprocessor_function) { if (function_exists($preprocessor_function)) { - $preprocessor_function($variables, $hook, $info); + $preprocessor_function($variables, $context); } } } @@ -609,7 +615,13 @@ // new variable to track that. if (!isset($variables['directory'])) { $default_template_variables = array(); - template_preprocess($default_template_variables, $hook, $info); + $context = array( + 'hook' => $hook, + 'suggestions' => $suggestions, + 'suggestion' => $suggestion, + 'info' => $info, + ); + template_preprocess($default_template_variables, $context); $variables += $default_template_variables; } if (!isset($default_attributes)) { @@ -1854,7 +1866,7 @@ * * @see _theme() */ -function template_preprocess(&$variables, $hook, $info) { +function template_preprocess(&$variables, $context) { // Tell all templates where they are located. $variables['directory'] = path_to_theme(); @@ -1873,8 +1885,8 @@ // When theming a render element, merge its #attributes into // $variables['attributes']. - if (isset($info['render element'])) { - $key = $info['render element']; + if (isset($context['info']['render element'])) { + $key = $context['info']['render element']; if (isset($variables[$key]['#attributes'])) { $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables[$key]['#attributes']); } @@ -2075,7 +2087,7 @@ * * @see drupal_render_page() */ -function template_preprocess_page(&$variables) { +function template_preprocess_page(&$variables, $context) { $language_interface = \Drupal::languageManager()->getCurrentLanguage(); $site_config = \Drupal::config('system.site'); @@ -2166,6 +2178,11 @@ '#breadcrumb' => \Drupal::service('breadcrumb')->build(\Drupal::request()->attributes->all()), ); } + + // @todo Remove once https://drupal.org/node/939462 is fixed. + if ($context['suggestion'] !== 'page' && function_exists($function = 'template_preprocess_' . $context['suggestion'])) { + $function($variables, $context); + } } /** @@ -2334,7 +2351,7 @@ * - content_attributes: A string containing the attributes for the content's * div. */ -function template_preprocess_field(&$variables, $hook) { +function template_preprocess_field(&$variables) { $element = $variables['element']; $variables['label_hidden'] = ($element['#label_display'] == 'hidden'); diff -u b/core/themes/bartik/bartik.theme b/core/themes/bartik/bartik.theme --- b/core/themes/bartik/bartik.theme +++ b/core/themes/bartik/bartik.theme @@ -14,7 +14,7 @@ * * Adds body classes if certain regions have content. */ -function bartik_preprocess_page(&$variables) { +function bartik_preprocess_page(&$variables, $context) { // Add information about the number of sidebars. /** @var \Drupal\Core\Page\HtmlPage $page_object */ $page_object = $variables['page']['#page']; @@ -88,6 +88,11 @@ // Make sure the shortcut link is the first item in title_suffix. $variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100; } + + // @todo Remove once https://drupal.org/node/939462 is fixed. + if ($context['suggestion'] !== 'page' && function_exists($function = 'bartik_preprocess_' . $context['suggestion'])) { + $function($variables, $context); + } } /** diff -u b/core/themes/seven/seven.theme b/core/themes/seven/seven.theme --- b/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -12,7 +12,7 @@ /** * Implements hook_preprocess_HOOK() for page templates. */ -function seven_preprocess_page(&$variables) { +function seven_preprocess_page(&$variables, $context) { /** @var \Drupal\Core\Page\HtmlPage $page_object */ $page_object = $variables['page']['#page']; $attributes = $page_object->getBodyAttributes(); @@ -34,6 +34,11 @@ '#theme' => 'menu_local_tasks', '#secondary' => isset($variables['tabs']['#secondary']) ? $variables['tabs']['#secondary'] : '', ); + + // @todo Remove once https://drupal.org/node/939462 is fixed. + if ($context['suggestion'] !== 'page' && function_exists($function = 'seven_preprocess_' . $context['suggestion'])) { + $function($variables, $context); + } } /** only in patch2: unchanged: --- a/core/modules/contextual/contextual.module +++ b/core/modules/contextual/contextual.module @@ -120,14 +120,14 @@ function contextual_element_info() { * @see contextual_page_build() * @see \Drupal\contextual\ContextualController::render() */ -function contextual_preprocess(&$variables, $hook, $info) { +function contextual_preprocess(&$variables, $context) { // Determine the primary theme function argument. - if (!empty($info['variables'])) { - $keys = array_keys($info['variables']); + if (!empty($context['info']['variables'])) { + $keys = array_keys($context['info']['variables']); $key = $keys[0]; } - elseif (!empty($info['render element'])) { - $key = $info['render element']; + elseif (!empty($context['info']['render element'])) { + $key = $context['info']['render element']; } if (!empty($key) && isset($variables[$key])) { $element = $variables[$key]; only in patch2: unchanged: --- a/core/modules/system/theme.api.php +++ b/core/modules/system/theme.api.php @@ -155,29 +155,32 @@ function hook_form_system_theme_settings_alter(&$form, &$form_state) { * * @param $variables * The variables array (modify in place). - * @param $hook - * The name of the theme hook. + * @param $context + * The context for which this preprocess function is running. This includes: + * - hook: + * @todo Document this after figuring out what it is. It's not necessarily + * the same as what was passed to _theme() as $hook, nor is it + * necessarily the same as 'suggestion'. What is it? + * - suggestions: The array of theme hook suggestions that were searched in + * order to find one that was implemented via a template or function. + * - suggestion: The theme hook suggestion that was found and therefore + * will be rendered after variable processing is complete. + * - info: The theme registry information for the matching hook/suggestion. */ -function hook_preprocess(&$variables, $hook) { - static $hooks; - +function hook_preprocess(&$variables, $context) { // Add contextual links to the variables, if the user has permission. if (!user_access('access contextual links')) { return; } - if (!isset($hooks)) { - $hooks = theme_get_registry(); - } - // Determine the primary theme function argument. - if (isset($hooks[$hook]['variables'])) { - $keys = array_keys($hooks[$hook]['variables']); + if (isset($context['info']['variables'])) { + $keys = array_keys($context['info']['variables']); $key = $keys[0]; } else { - $key = $hooks[$hook]['render element']; + $key = $context['info']['render element']; } if (isset($variables[$key])) {