diff --git a/core/includes/form.inc b/core/includes/form.inc index 5d4a943..f941efb 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2177,7 +2177,7 @@ function form_process_autocomplete($element, &$form_state) { */ function template_preprocess_input(&$variables) { $element = $variables['element']; - $variables['attributes'] = new Attribute($element['#attributes']); + $variables['attributes'] = $element['#attributes']; } /** diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 144882b..ef80fd0 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 ba915a9..200c117 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -8,6 +8,7 @@ use Drupal\block\BlockInterface; use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Template\Attribute; use Symfony\Cmf\Component\Routing\RouteObjectInterface; /** @@ -506,6 +507,9 @@ function template_preprocess_block(&$variables) { // them through to the content's #theme function/template. This allows the // content to not require a function/template at all, or if it does use one, // to not require it to output an extra wrapping element. + if(!isset($variables['content_attributes'])) { + $variables['content_attributes'] = array(); + } if (isset($variables['content']['#attributes'])) { $variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']); unset($variables['content']['#attributes']); @@ -518,6 +522,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/comment/comment.module b/core/modules/comment/comment.module index 25607de..2cc3be5 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -13,6 +13,7 @@ use Drupal\Component\Utility\Number; 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; @@ -1497,6 +1498,15 @@ function template_preprocess_comment(&$variables) { $variables['attributes']['data-comment-user-id'] = $comment->uid->value; $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/datetime/datetime.module b/core/modules/datetime/datetime.module index ccf1a0c..5f16270 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -200,7 +200,6 @@ function datetime_date_default_time($date) { function template_preprocess_datetime_form(&$variables) { $element = $variables['element']; - $variables['attributes'] = array(); if (isset($element['#id'])) { $variables['attributes']['id'] = $element['#id']; } 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 bf79da6..35e91e6 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -705,6 +705,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; }