diff --git a/core/includes/theme.inc b/core/includes/theme.inc index de8131b..720f806 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1203,7 +1203,7 @@ function template_preprocess(&$variables, $hook, $info) { $render_element_type = isset($info['render element']) ? $info['render element'] : NULL; $themeManager = \Drupal::service('theme.manager'); - $variables += $themeManager->getDefaultTemplateVariables($render_element_type); + $variables += $themeManager->getDefaultTemplateVariables($render_element_type, $variables); } /** diff --git a/core/lib/Drupal/Core/Theme/Registry.php b/core/lib/Drupal/Core/Theme/Registry.php index a8a0ae0..f389990 100644 --- a/core/lib/Drupal/Core/Theme/Registry.php +++ b/core/lib/Drupal/Core/Theme/Registry.php @@ -511,7 +511,6 @@ protected function processExtension(array &$cache, $name, $type, $theme, $path) if (!isset($info['preprocess functions']) || !is_array($info['preprocess functions'])) { $info['preprocess functions'] = array(); $prefixes = array(); - if ($type == 'module') { // Add template_preprocess_HOOK functions. if (function_exists('template_preprocess_' . $hook)) { diff --git a/core/lib/Drupal/Core/Theme/ThemeManager.php b/core/lib/Drupal/Core/Theme/ThemeManager.php index 7bf0e30..7f96d32 100644 --- a/core/lib/Drupal/Core/Theme/ThemeManager.php +++ b/core/lib/Drupal/Core/Theme/ThemeManager.php @@ -3,12 +3,12 @@ namespace Drupal\Core\Theme; use Drupal\Component\Render\MarkupInterface; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Render\Markup; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\StackedRouteMatchInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Template\Attribute; -use Drupal\Component\Utility\NestedArray; /** * Provides the default implementation of a theme manager. @@ -284,8 +284,16 @@ public function render($hook, array $variables) { } // Set default variables before preprocess hooks. - $render_element_type = isset($info['render element']) ? $info['render element'] : NULL; - $variables += $this->getDefaultTemplateVariables($render_element_type); + $variables += $this->getDefaultTemplateVariables(); + + // Merge element #attributes into $default_variables['attributes'] if they + // exist in render_element. + if (isset($element['#attributes'])) { + $variables['attributes'] = NestedArray::mergeDeep( + $variables['attributes'], + $element['#attributes'] + ); + } // Invoke preprocess hooks. // By default $info['preprocess functions'] should always be set, but it's @@ -459,37 +467,29 @@ public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) { /** * {@inheritdoc} */ - public function getDefaultTemplateVariables($render_element_type) { + public function getDefaultTemplateVariables() { static $drupal_static_fast; if (!isset($drupal_static_fast)) { - $drupal_static_fast['default_variables'] = &drupal_static('getDefaultTemplateVariables'); + $drupal_static_fast['default_variables'] = &drupal_static(__METHOD__); } $default_variables = &$drupal_static_fast['default_variables']; - if(!isset($default_variables)) { + if (!isset($default_variables)) { // Variables that don't depend on a database connection. - $default_variables = array( - 'attributes' => array(), - 'title_attributes' => array(), - 'content_attributes' => array(), - 'title_prefix' => array(), - 'title_suffix' => array(), + $default_variables = [ + 'attributes' => [], + 'title_attributes' => [], + 'content_attributes' => [], + 'title_prefix' => [], + 'title_suffix' => [], 'db_is_active' => !defined('MAINTENANCE_MODE'), 'is_admin' => FALSE, 'logged_in' => FALSE, - ); + ]; // Give modules a chance to alter default template variables. \Drupal::moduleHandler()->alter('template_preprocess_default_variables', $default_variables); // Tell all templates where they are located. - $default_variables['directory'] = \Drupal::theme()->getActiveTheme()->getPath(); - } - // Merge element #attributes into $default_variables['attributes'] if they - // exist in render_element. - if (isset($default_variables[$render_element_type]['#attributes'])) { - $default_variables['attributes'] = NestedArray::mergeDeep( - $default_variables['attributes'], - $default_variables[$render_element_type]['#attributes'] - ); + $default_variables['directory'] = $this->getActiveTheme()->getPath(); } return $default_variables; } diff --git a/core/lib/Drupal/Core/Theme/ThemeManagerInterface.php b/core/lib/Drupal/Core/Theme/ThemeManagerInterface.php index 74bf3e8..42bf65c 100644 --- a/core/lib/Drupal/Core/Theme/ThemeManagerInterface.php +++ b/core/lib/Drupal/Core/Theme/ThemeManagerInterface.php @@ -153,13 +153,9 @@ public function alterForTheme(ActiveTheme $theme, $type, &$data, &$context1 = NU * See the @link themeable Default theme implementations topic @endlink for * details. * - * @see template_preprocess() - * - * @param string $render_element_type - * The type of the element to render. E.g. 'html', 'page', 'element', - * 'elements, 'form'... - * @return + * @return array * An array of default template variables. */ - public function getDefaultTemplateVariables($render_element_type); + public function getDefaultTemplateVariables(); + }