diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 2b06429..0624722 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -408,8 +408,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 @@ -615,19 +613,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. @@ -1061,8 +1048,6 @@ function template_preprocess_datetime(&$variables) { // Add a 'datetime' class. $variables['attributes']['class'][] = 'datetime'; - - $variables['attributes'] = new Attribute($variables['attributes']); } /** @@ -1891,8 +1876,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'), @@ -2387,11 +2370,6 @@ function template_preprocess_field(&$variables, $hook) { $variables['attributes']['class'][] = 'clearfix'; } - 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 @@ -2400,7 +2378,7 @@ 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(); } } diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 37b93bd..32b2554 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -8,6 +8,7 @@ use Drupal\block\BlockInterface; use Drupal\language\Entity\Language; use Drupal\system\Entity\Menu; +use Drupal\Core\Template\Attribute; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; @@ -382,6 +383,10 @@ function template_preprocess_block(&$variables) { if ($id = $variables['elements']['#block']->id()) { $variables['attributes']['id'] = drupal_html_id('block-' . $id); } + + // For best performance, we only instantiate Attribute objects when needed. + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : NULL; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : NULL; } /** diff --git a/core/modules/block/src/Tests/BlockPreprocessUnitTest.php b/core/modules/block/src/Tests/BlockPreprocessUnitTest.php index e343019..d12c88a 100644 --- a/core/modules/block/src/Tests/BlockPreprocessUnitTest.php +++ b/core/modules/block/src/Tests/BlockPreprocessUnitTest.php @@ -55,7 +55,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 b09bfa8..8537b97 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -19,6 +19,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\FieldInstanceConfigInterface; use Drupal\field\FieldConfigInterface; @@ -1463,6 +1464,10 @@ 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']) ? $variables['title_attributes'] : array(); + $variables['title_attributes'] = new Attribute($variables['title_attributes']); } /** diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 2253f71..f1b6f93 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -701,6 +701,8 @@ function template_preprocess_node(&$variables) { $variables['attributes']['class'][] = 'preview'; } $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(); } /** diff --git a/core/modules/search/search.pages.inc b/core/modules/search/search.pages.inc index 34b235c..81e2644 100644 --- a/core/modules/search/search.pages.inc +++ b/core/modules/search/search.pages.inc @@ -6,6 +6,7 @@ */ use Drupal\Core\Language\Language; +use Drupal\Core\Template\Attribute; /** * Implements hook_theme_suggestions_HOOK(). @@ -59,5 +60,8 @@ 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(); }