diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f7acb39..ef2e320 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -416,8 +416,6 @@ function list_themes($refresh = FALSE) { * @see template_preprocess() */ function _theme($hook, $variables = array()) { - static $default_attributes; - $module_handler = \Drupal::moduleHandler(); // If called before all modules are loaded, we do not necessarily have a full @@ -623,19 +621,8 @@ function _theme($hook, $variables = array()) { template_preprocess($default_template_variables, $hook, $info); $variables += $default_template_variables; } - if (!isset($default_attributes)) { - $default_attributes = new Attribute(); - } - foreach (array('attributes', 'title_attributes', 'content_attributes') as $key) { - if (isset($variables[$key]) && !($variables[$key] instanceof Attribute)) { - if ($variables[$key]) { - $variables[$key] = new Attribute($variables[$key]); - } - else { - // Create empty attributes. - $variables[$key] = clone $default_attributes; - } - } + if (isset($variables['attributes']) && !($variables['attributes'] instanceof Attribute)) { + $variables['attributes'] = new Attribute($variables['attributes']); } // Render the output using the template file. @@ -1088,8 +1075,6 @@ function template_preprocess_datetime(&$variables) { // Add a 'datetime' class. $variables['attributes']['class'][] = 'datetime'; - - $variables['attributes'] = new Attribute($variables['attributes']); } /** @@ -1932,8 +1917,6 @@ function _template_preprocess_default_variables() { // Variables that don't depend on a database connection. $variables = array( 'attributes' => array(), - 'title_attributes' => array(), - 'content_attributes' => array(), 'title_prefix' => array(), 'title_suffix' => array(), 'db_is_active' => !defined('MAINTENANCE_MODE'), @@ -2418,11 +2401,6 @@ function template_preprocess_field(&$variables, $hook) { $variables['title_attributes']['class'][] = 'visually-hidden'; } - static $default_attributes; - if (!isset($default_attributes)) { - $default_attributes = new Attribute; - } - // Modules (e.g., rdf.module) can add field item attributes (to // $item->_attributes) within hook_entity_prepare_view(). Some field // formatters move those attributes into some nested formatter-specific @@ -2431,8 +2409,11 @@ function template_preprocess_field(&$variables, $hook) { // formatters leave them within $element['#items'][$delta]['_attributes'] to // be rendered on the item wrappers provided by field.html.twig. foreach ($variables['items'] as $delta => $item) { - $variables['item_attributes'][$delta] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone($default_attributes); + $variables['item_attributes'][$delta] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : new Attribute(); } + + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : new Attribute(); + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : new Attribute(); } /** diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 78d9fad..96a075b 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -9,6 +9,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\system\Entity\Menu; +use Drupal\Core\Template\Attribute; /** * Implements hook_help(). @@ -279,9 +280,6 @@ function template_preprocess_block(&$variables) { $variables['attributes']['class'][] = 'block'; $variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['provider']); - // Add default class for block content. - $variables['content_attributes']['class'][] = 'content'; - // Create a valid HTML ID and make sure it is unique. if (!empty($variables['elements']['#id'])) { $variables['attributes']['id'] = drupal_html_id('block-' . $variables['elements']['#id']); @@ -293,6 +291,11 @@ function template_preprocess_block(&$variables) { $variables['attributes']['aria-describedby'] = $variables['title_attributes']['id']; } + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : new Attribute(); + + // Add default class for block content. + $variables['content_attributes']['class'][] = 'content'; + $variables['content_attributes'] = new Attribute($variables['content_attributes']); } /** diff --git a/core/modules/block/src/Tests/BlockPreprocessUnitTest.php b/core/modules/block/src/Tests/BlockPreprocessUnitTest.php index 9118e21..f98bbb3 100644 --- a/core/modules/block/src/Tests/BlockPreprocessUnitTest.php +++ b/core/modules/block/src/Tests/BlockPreprocessUnitTest.php @@ -50,7 +50,8 @@ function testBlockClasses() { // Test adding a class to the block content. $variables['content_attributes']['class'][] = 'test-class'; template_preprocess_block($variables); - $this->assertEqual($variables['content_attributes']['class'], array('test-class', 'content'), 'Default .content class added to block content_attributes'); + $classes = $variables['content_attributes']['class']->value(); + $this->assertEqual($classes, array('test-class', 'content'), 'Default .content class added to block content_attributes'); } } diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 5ce3ba9..b6cd321 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -24,6 +24,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Render\Element; +use Drupal\Core\Template\Attribute; use Drupal\Core\Url; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\FieldInstanceConfigInterface; @@ -988,6 +989,8 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['data-comment-user-id'] = $comment->getOwnerId(); $variables['content_attributes']['class'][] = 'content'; + $variables['content_attributes'] = new Attribute($variables['content_attributes']); + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : new Attribute(); } /** @@ -1012,7 +1015,6 @@ function comment_preprocess_field(&$variables) { // Adjust a comment field's attributes. $variables['attributes']['class'][] = 'comment-wrapper'; - $variables['title_attributes']['class'][] = 'title'; // Append additional attributes (eg. RDFa) from the first field item. $variables['attributes'] += $variables['item_attributes'][0]->storage(); @@ -1020,7 +1022,8 @@ function comment_preprocess_field(&$variables) { // Create separate variables for the comments and comment form. $variables['comments'] = $element[0]['comments']; $variables['comment_form'] = $element[0]['comment_form']; - $variables['content_attributes']['class'] = array('title', 'comment-form__title'); + $variables['title_attributes']->addClass('title'); + $variables['content_attributes']->addClass(array('title', 'comment-form__title')); } } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index fefd6d5..cd4d58c 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -672,6 +672,8 @@ function template_preprocess_node(&$variables) { if (isset($variables['preview'])) { $variables['attributes']['class'][] = 'node--preview'; } + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : new Attribute(); + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : new Attribute(); } /** diff --git a/core/modules/search/search.module b/core/modules/search/search.module index be8da8a..5c0e701 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -132,7 +132,7 @@ function search_permission() { function search_preprocess_block(&$variables) { if ($variables['plugin_id'] == 'search_form_block') { $variables['attributes']['role'] = 'search'; - $variables['content_attributes']['class'][] = 'container-inline'; + $variables['content_attributes']->addClass('container-inline'); } } diff --git a/core/modules/search/search.pages.inc b/core/modules/search/search.pages.inc index 6590b90..13f688c 100644 --- a/core/modules/search/search.pages.inc +++ b/core/modules/search/search.pages.inc @@ -7,6 +7,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Template\Attribute; /** * Implements hook_theme_suggestions_HOOK(). @@ -60,5 +61,7 @@ function template_preprocess_search_result(&$variables) { // Provide separated and grouped meta information.. $variables['info_split'] = $info; $variables['info'] = implode(' - ', $info); + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : new Attribute(); + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : new Attribute(); } diff --git a/core/themes/bartik/bartik.theme b/core/themes/bartik/bartik.theme index 27f8973..e78d263 100644 --- a/core/themes/bartik/bartik.theme +++ b/core/themes/bartik/bartik.theme @@ -158,9 +158,9 @@ function bartik_menu_tree__shortcut_default($variables) { function bartik_preprocess_field(&$variables) { $element = $variables['element']; if ($element['#field_type'] == 'taxonomy_term_reference') { - $variables['title_attributes']['class'][] = 'field-label'; + $variables['title_attributes']->addClass('field-label'); if ($variables['element']['#label_display'] == 'inline') { - $variables['title_attributes']['class'][] = 'inline'; + $variables['title_attributes']->addClass('inline'); } } }