diff --git a/core/includes/theme.inc b/core/includes/theme.inc index efd7d23..89ca981 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -718,15 +718,14 @@ function theme($hook, $variables = array()) { 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)) { + if ($variables['attributes']) { + $variables['attributes'] = new Attribute($variables['attributes']); + } + else { + // Create empty attributes. + $variables['attributes'] = clone $default_attributes; } } @@ -1143,8 +1142,6 @@ function template_preprocess_datetime(&$variables) { // Add a 'datetime' class. $variables['attributes']['class'][] = 'datetime'; - - $variables['attributes'] = new Attribute($variables['attributes']); } /** @@ -2035,8 +2032,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'), diff --git a/core/modules/block/block.module b/core/modules/block/block.module index daea82c..4f56dd7 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -7,6 +7,7 @@ use Drupal\block\BlockInterface; use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Core\Template\Attribute; use Symfony\Cmf\Component\Routing\RouteObjectInterface; /** @@ -490,6 +491,15 @@ function template_preprocess_block(&$variables) { if ($id = $variables['elements']['#block']->id()) { $variables['attributes']['id'] = drupal_html_id('block-' . $id); } + + static $default_attributes; + if (!isset($default_attributes)) { + $default_attributes = new Attribute; + } + + // For best performance, we only instantiate Attribute objects when needed. + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone $default_attributes; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone $default_attributes; } /** diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockPreprocessUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockPreprocessUnitTest.php index e343019..d12c88a 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockPreprocessUnitTest.php +++ b/core/modules/block/lib/Drupal/block/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 1ee4ebc..533cc6a 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityChangedInterface; +use Drupal\Core\Template\Attribute; use Drupal\comment\CommentInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\field\FieldInstanceInterface; @@ -1504,6 +1505,15 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['data-comment-user-id'] = $comment->getOwnerId(); $variables['content_attributes']['class'][] = 'content'; + + static $default_attributes; + if (!isset($default_attributes)) { + $default_attributes = new Attribute; + } + + // For best performance, we only instantiate Attribute objects when needed. + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone $default_attributes; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone $default_attributes; } /** diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 0aaffb5..6b394df 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -556,16 +556,11 @@ function template_preprocess_field(&$variables, $hook) { if (!isset($default_attributes)) { $default_attributes = new Attribute; } - // The default theme implementation for fields is a function. - // template_preprocess() (which initializes the attributes, title_attributes, - // and content_attributes arrays) does not run for theme function - // implementations. Additionally, Attribute objects for the three variables - // below only get instantiated for template file implementations, and we need - // Attribute objects for printing in both theme functions and template files. + // For best performance, we only instantiate Attribute objects when needed. $variables['attributes'] = isset($variables['attributes']) ? new Attribute($variables['attributes']) : clone $default_attributes; - $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone($default_attributes); - $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone($default_attributes); + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone $default_attributes; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone $default_attributes; // Modules (e.g., rdf.module) can add field item attributes (to // $item->_attributes) within hook_entity_prepare_view(). Some field @@ -575,7 +570,7 @@ function template_preprocess_field(&$variables, $hook) { // formatters leave them within $element['#items'][$delta]['_attributes'] to // be rendered on the item wrappers provided by theme_field(). 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) : clone $default_attributes; } } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index c22abe3..668efae 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -707,6 +707,16 @@ function template_preprocess_node(&$variables) { $variables['attributes']['class'][] = 'preview'; } $variables['content_attributes']['class'][] = 'content'; + + static $default_attributes; + if (!isset($default_attributes)) { + $default_attributes = new Attribute; + } + + // For best performance, we only instantiate Attribute objects when needed. + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone $default_attributes; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone $default_attributes; + } /** diff --git a/core/modules/search/search.pages.inc b/core/modules/search/search.pages.inc index 023a825..2e29ceb 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; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -88,5 +89,14 @@ function template_preprocess_search_result(&$variables) { // Provide separated and grouped meta information.. $variables['info_split'] = $info; $variables['info'] = implode(' - ', $info); + + static $default_attributes; + if (!isset($default_attributes)) { + $default_attributes = new Attribute; + } + + // For best performance, we only instantiate Attribute objects when needed. + $variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone $default_attributes; + $variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone $default_attributes; }