diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 0384456..4e37a2a 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -830,38 +830,20 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * engine, it overrides the default implementation of the 'page' theme hook by * containing a 'page.html.twig' file within its folder structure). * - * @todo Add/edit/remove these subsections with hook_theme_prepare(). - * @subsection sub_preprocess_templates Preprocessing for Template Files - * If the implementation is a template file, several functions are called before - * the template file is invoked to modify the $variables array. These make up - * the "preprocessing" phase, and are executed (if they exist), in the following - * order (note that in the following list, HOOK indicates the theme hook name, - * MODULE indicates a module name, THEME indicates a theme name, and ENGINE - * indicates a theme engine name): - * - template_preprocess(&$variables, $hook): Creates a default set of variables - * for all theme hooks with template implementations. - * - template_preprocess_HOOK(&$variables): Should be implemented by the module - * that registers the theme hook, to set up default variables. - * - MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all - * implementing modules. - * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on - * all implementing modules, so that modules that didn't define the theme hook - * can alter the variables. - * - ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to - * set necessary variables for all theme hooks with template implementations. - * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set - * necessary variables for the particular theme hook. - * - THEME_preprocess(&$variables, $hook): Allows the theme to set necessary - * variables for all theme hooks with template implementations. - * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary - * variables specific to the particular theme hook. - * - * @subsection sub_preprocess_theme_funcs Preprocessing for Theme Functions - * If the implementation is a function, only the theme-hook-specific preprocess - * functions (the ones ending in _HOOK) are called from the list above. This is - * because theme hooks with function implementations need to be fast, and - * calling the non-theme-hook-specific preprocess functions for them would incur - * a noticeable performance penalty. + * @subsection sub_prepare_variables Preparing Variables for Theme Hooks + * Several functions are invoked before theme functions or template files are + * provided their available $variables. These functions make up the "prepare" + * phase, and are executed (if they exist), in the following + * order (HOOK indicates the profile, module, theme engine or theme name, + * THEME_HOOK indicates the name of the specific theme hook called): + * - HOOK_theme_prepare(&$variables, $context): Prepare a set of variables + * for all theme hook implementations. + * - HOOK_theme_prepare_THEME_HOOK(&$variables, $context): Prepare a set of + * variables for a specific theme hook implementation. + * - HOOK_theme_prepare_alter(&$variables, $context): Alter a set of prepared + * variables for all theme hook implementations. + * - HOOK_theme_prepare_THEME_HOOK_alter(&$variables, $context): Alter a set of + * prepared variables for a specific theme hook implementation. * * @subsection sub_alternate_suggestions Suggesting Alternate Hooks * Alternate hooks can be suggested by implementing the hook-specific @@ -877,11 +859,11 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * * @param $hook * The name of the theme hook to call. If the name contains a - * double-underscore ('__') and there isn't an implementation for the full - * name, the part before the '__' is checked. This allows a fallback to a + * double-underscore ('__') and there is not an implementation for the full + * name, the name before the '__' is checked. This allows a fallback to a * more generic implementation. For example, if theme('links__node', ...) is * called, but there is no implementation of that theme hook, then the - * 'links' implementation is used. This process is iterative, so if + * 'links' implementation is used instead. This process is iterative, so if * theme('links__contextual__node', ...) is called, theme() checks for the * following implementations, and uses the first one that exists: * - links__contextual__node @@ -895,10 +877,14 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * convention is not desired or is insufficient. * @param $variables * An associative array of variables to merge with defaults from the theme - * registry, pass to preprocess functions for modification, and finally, pass + * registry, pass to prepare functions for modification, and finally, pass * to the function or template implementing the theme hook. Alternatively, * this can be a renderable array, in which case, its properties are mapped to * variables expected by the theme hook implementations. + * @param array $context + * An associative array of data used as context in hook_theme_prepare(). This + * array is used internally and is not available as a "variable" in theme + * functions or templates. * * @return string|false * An HTML string representing the themed output or FALSE if the passed $hook @@ -911,7 +897,7 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * * @throws Exception */ -function theme($hook, $variables = array()) { +function theme($hook, $variables = array(), $context = array()) { static $default_attributes; // If called before all modules are loaded, we do not necessarily have a full // theme registry to work with, and therefore cannot process the theme diff --git a/core/modules/system/tests/modules/theme_test/theme_test.module b/core/modules/system/tests/modules/theme_test/theme_test.module index 8e55fc5..bab08b3 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.module +++ b/core/modules/system/tests/modules/theme_test/theme_test.module @@ -194,7 +194,7 @@ function theme_theme_test_prepare_alter_variable($variables) { */ function theme_test_theme_prepare($context) { $variables = array(); - if ($context['theme_id'] == 'theme_test_prepare_variable' || $context['theme_id'] == 'theme_test_prepare_alter_variable') { + if ($context['theme_hook'] == 'theme_test_prepare_variable' || $context['theme_hook'] == 'theme_test_prepare_alter_variable') { $variables['element'] = array( 'variable1' => array( '#markup' => '
Inserted variable1 via hook_theme_prepare().
', @@ -234,7 +234,7 @@ function theme_test_theme_prepare_theme_test_prepare_alter_variable() { * Implements hook_theme_prepare_alter(). */ function theme_test_theme_prepare_alter(&$variables, $context) { - if ($context['theme_id'] == 'theme_test_prepare_alter_variable') { + if ($context['theme_hook'] == 'theme_test_prepare_alter_variable') { $variables['element']['variable1']['#markup'] = 'Inserted variable1 via hook_theme_prepare() was altered by hook_theme_prepare_alter().
'; } } diff --git a/core/modules/system/tests/themes/test_theme/test_theme.theme b/core/modules/system/tests/themes/test_theme/test_theme.theme index ac559ec..2fec7d3 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.theme +++ b/core/modules/system/tests/themes/test_theme/test_theme.theme @@ -75,7 +75,7 @@ function test_theme_theme_test_function_suggestions__module_override($variables) */ function test_theme_theme_prepare($context) { $variables = array(); - if ($context['theme_id'] == 'theme_test_prepare_variable' || $context['theme_id'] == 'theme_test_prepare_alter_variable') { + if ($context['theme_hook'] == 'theme_test_prepare_variable' || $context['theme_hook'] == 'theme_test_prepare_alter_variable') { $variables['element'] = array( 'themeVariable1' => array( '#markup' => 'Inserted themeVariable1 via theme hook_theme_prepare().
', @@ -86,7 +86,7 @@ function test_theme_theme_prepare($context) { } /** - * Implements hook_theme_prepare_BASE_THEME_ID(). + * Implements hook_theme_prepare_THEME_HOOK(). */ function test_theme_theme_prepare_theme_test_prepare_variable() { $variables = array(); @@ -99,7 +99,7 @@ function test_theme_theme_prepare_theme_test_prepare_variable() { } /** - * Implements hook_theme_prepare_BASE_THEME_ID(). + * Implements hook_theme_prepare_THEME_HOOK(). */ function test_theme_theme_prepare_theme_test_prepare_alter_variable() { $variables = array(); @@ -115,7 +115,7 @@ function test_theme_theme_prepare_theme_test_prepare_alter_variable() { * Implements hook_theme_prepare_alter(). */ function test_theme_theme_prepare_alter(&$variables, $context) { - if ($context['theme_id'] == 'theme_test_prepare_alter_variable') { + if ($context['theme_hook'] == 'theme_test_prepare_alter_variable') { $variables['element']['themeVariable1']['#markup'] = 'Inserted themeVariable1 via theme hook_theme_prepare() was altered by theme hook_theme_prepare_alter().
'; } }