diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 13ce524..0384456 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -830,6 +830,7 @@ 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 @@ -907,6 +908,8 @@ function drupal_find_base_themes($themes, $key, $used_keys = array()) { * @see themeable * @see hook_theme() * @see template_preprocess() + * + * @throws Exception */ function theme($hook, $variables = array()) { static $default_attributes; @@ -1058,8 +1061,8 @@ function theme($hook, $variables = array()) { // Provide immutable context for subsequent hooks. $context_original = array( - 'theme_id' => $hook, - 'original_theme_id' => $original_hook, + 'theme_hook' => $hook, + 'original_theme_hook' => $original_hook, 'suggestions' => $suggestions, ); @@ -1074,7 +1077,7 @@ function theme($hook, $variables = array()) { $prepare_hooks[] = 'theme_prepare_' . $original_hook; } - // Invokes hook_theme_prepare(), hook_theme_prepare_THEME_ID() for enabled + // Invokes hook_theme_prepare(), hook_theme_prepare_THEME_HOOK() for enabled // modules. foreach ($prepare_hooks as $prepare_hook) { $context = $context_original; @@ -1090,8 +1093,7 @@ function theme($hook, $variables = array()) { $variables = NestedArray::mergeDeep($variables, (array) Drupal::moduleHandler()->themeInvoke($prepare_hook, array($context))); } - // Invokes preprocess functions. - // @todo Deprecate @see https://drupal.org/node/2060773 + // Invokes preprocess functions (deprecated). if (isset($info['preprocess functions'])) { foreach ($info['preprocess functions'] as $preprocessor_function) { if (function_exists($preprocessor_function)) { diff --git a/core/modules/system/theme.api.php b/core/modules/system/theme.api.php index 34758da..3ca5861 100644 --- a/core/modules/system/theme.api.php +++ b/core/modules/system/theme.api.php @@ -104,9 +104,15 @@ function hook_form_system_theme_settings_alter(&$form, &$form_state) { * The variables array (modify in place). * @param $hook * The name of the theme hook. + * + * @deprecated + * @see hook_theme_prepare() */ function hook_preprocess(&$variables, $hook) { - static $hooks; + // This hook has been deprecated, do not use it. Please use instead: + // hook_theme_prepare(). + + static $hooks; // Add contextual links to the variables, if the user has permission. @@ -150,14 +156,138 @@ function hook_preprocess(&$variables, $hook) { * * @param $variables * The variables array (modify in place). + * + * @deprecated + * @see hook_theme_prepare_SUGGESTION() */ function hook_preprocess_HOOK(&$variables) { + // This hook has been deprecated, do not use it. Please use instead: + // hook_theme_prepare_SUGGESTION(). + // This example is from rdf_preprocess_image(). It adds an RDF attribute // to the image hook's variables. $variables['attributes']['typeof'] = array('foaf:Image'); } /** + * Prepares theme variables for templates. + * + * This hook allows modules to prepare theme variables for theme templates. + * It is called for all theme hooks implemented as templates, but not for theme + * hooks implemented as functions. hook_preprocess_HOOK() can be used to + * preprocess variables for a specific theme hook, whether implemented as a + * template or function. + * + * For more detailed information, see theme(). + * + * @param array $variables + * The variables array (modify in place). + * @param array $context + * The immutable context array. + */ +function hook_theme_prepare(&$variables, $context) { + static $hooks; + + // 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(); + } + + // Retrieve the original theme hook from context. + $hook = $context['original_theme_hook']; + + // Determine the primary theme function argument. + if (isset($hooks[$hook]['variables'])) { + $keys = array_keys($hooks[$hook]['variables']); + $key = $keys[0]; + } + else { + $key = $hooks[$hook]['render element']; + } + + if (isset($key) && !empty($variables[$key])) { + $element = $variables[$key]; + } + + if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { + $variables['title_suffix']['contextual_links'] = contextual_links_view($element); + if (!empty($variables['title_suffix']['contextual_links'])) { + $variables['attributes']['class'][] = 'contextual-links-region'; + } + } +} + +/** + * Alters the prepared theme variables used in templates. + * + * This hook allows any module or theme to add new variables or reorder/remove + * existing variables provided by earlier invocations of hook_theme_prepare() or + * hook_theme_prepare_THEME_HOOK(). + * + * For more detailed information, see theme(). + * + * @param array $variables + * The variables array (modify in place). + * @param array $context + * The immutable context array. + */ +function hook_theme_prepare_alter(&$variables, $context) { + // Replace the name of the user whom authored the node. + if ($context['theme_hook'] === 'node') { + $variables['name'] = t('Anonymous Unicorn'); + } +} + +/** + * Prepares variables used in templates for a specific theme suggestion. + * + * This hook allows modules to prepare theme variables for a specific theme + * suggestion. It should only be used when it is needed to override or add to + * theme preparation for a theme hook it did not define. + * + * THEME_HOOK can be either the base theme hook or a more specific suggestion + * using the double-underscore ('__') notation. + * + * For more detailed information, see theme(). + * + * @param $variables + * The variables array (modify in place). + * @param array $context + * The immutable context array. + */ +function hook_theme_prepare_THEME_HOOK(&$variables, $context) { + // This example is from rdf_theme_prepare_image(). It adds an RDF attribute + // to the image hook's variables. + $variables['attributes']['typeof'] = array('foaf:Image'); +} + +/** + * Alters prepared variables used in templates for a specific theme suggestion. + * + * This hook allows any module or theme to add new variables or reorder/remove + * existing variables provided by earlier invocations of hook_theme_prepare() or + * hook_theme_prepare_THEME_HOOK(). + * + * THEME_HOOK can be either the base theme hook or a more specific suggestion + * using the double-underscore ('__') notation. + * + * For more detailed information, see theme(). + * + * @param array $variables + * The variables array (modify in place). + * @param array $context + * The immutable context array. + */ +function hook_theme_prepare_THEME_HOOK_alter(&$variables, $context) { + // Replace the name of the user whom authored the node. + $variables['name'] = t('Anonymous Unicorn'); +} + +/** * Provides alternate named suggestions for a specific theme hook. * * This hook allows the module implementing hook_theme() for a theme hook to @@ -173,8 +303,8 @@ function hook_preprocess_HOOK(&$variables) { * @todo Add @code sample. * * @param array $variables - * An array of variables passed to the theme hook. Note that this hook is - * invoked before any preprocessing. + * An array of variables passed to the theme hook. NOTE: this hook is invoked + * prior to hook_theme_prepare(). Some variables may not always be available. * * @return array * An array of theme suggestions.