diff --git a/core/lib/Drupal/Core/Entity/EntityRenderController.php b/core/lib/Drupal/Core/Entity/EntityRenderController.php index 860c044..5912d67 100644 --- a/core/lib/Drupal/Core/Entity/EntityRenderController.php +++ b/core/lib/Drupal/Core/Entity/EntityRenderController.php @@ -31,6 +31,16 @@ public function __construct($entity_type) { */ public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) { field_attach_prepare_view($this->entityType, $entities, $displays, $langcode); + + // Initialize the field item attributes for the fields set to be displayed. + foreach ($entities as $entity) { + foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) { + foreach ($entity->get($name) as $item) { + $item->_attributes = array(); + } + } + } + module_invoke_all('entity_prepare_view', $this->entityType, $entities, $displays, $view_mode); foreach ($entities as $entity) { diff --git a/core/modules/field/field.module b/core/modules/field/field.module index cc7ad90..d47e7f2 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -873,6 +873,18 @@ function template_preprocess_field(&$variables, $hook) { foreach ($variables['items'] as $delta => $item) { $variables['item_attributes'][$delta] = isset($variables['item_attributes'][$delta]) ? new Attribute($variables['item_attributes'][$delta]) : clone($default_attributes); } + + // Field formatters get the HTML attributes as part of each field item + // in case they want to inject these attributes in their HTML output. + // If the field formatter did not include these attributes in its HTML and + // left them in the item, add these attributes to the field template. + // @todo should we use $variables['items'] here instead of $element['#items']? + foreach ($element['#items'] as $delta => $item) { + if (!empty($item['_attributes'])) { + // Merge HTML data attributes with item_attributes. + $variables['item_attributes'][$delta] = new Attribute($item['_attributes']); + } + } } /** diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php index 57eb6ae..af2ef0d 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/GenericFileFormatter.php @@ -38,13 +38,13 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface '#file' => $item->entity, '#description' => $item->description, ); - // Pass HTML data attributes to the theme function. - if (isset($item->html_data_attributes)) { + // Pass field item attributes to the theme function. + if (isset($item->_attributes)) { $elements[$delta] += array('#attributes' => array()); - $elements[$delta]['#attributes'] = $item->html_data_attributes; - // Unset HTML data attributes since they've been added to the - // formatter and thus should not be rendered in the field template. - unset($item->html_data_attributes); + $elements[$delta]['#attributes'] += $item->_attributes; + // Unset field item attributes since they have been included in the + // formatter output and should not be rendered in the field template. + unset($item->_attributes); } } } diff --git a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php index a1398e8..84a0c8c 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php +++ b/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php @@ -120,13 +120,13 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface '#image_style' => $image_style_setting, '#path' => isset($uri) ? $uri : '', ); - // Pass HTML data attributes to the theme function. - if (isset($item->html_data_attributes)) { + // Pass field item attributes to the theme function. + if (isset($item->_attributes)) { $elements[$delta]['#item'] += array('attributes' => array()); - $elements[$delta]['#item']['attributes'] += $item->html_data_attributes; - // Unset HTML data attributes since they've been added to the - // formatter and thus should not be rendered in the field template. - unset($item->html_data_attributes); + $elements[$delta]['#item']['attributes'] += $item->_attributes; + // Unset field item attributes since they have been included in the + // formatter output and should not be rendered in the field template. + unset($item->_attributes); } } } diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index bf2554e..85fa1e9 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -210,7 +210,7 @@ function rdf_entity_prepare_view($entity_type, array $entities, array $displays) $field_mapping = $mapping->getPreparedFieldMapping($name); if ($field_mapping['properties']) { foreach ($entity->get($name) as $item) { - $item->html_data_attributes = rdf_rdfa_attributes($field_mapping, $item->getValue()); + $item->_attributes += rdf_rdfa_attributes($field_mapping, $item->getValue()); } } } @@ -339,22 +339,6 @@ function rdf_preprocess_node(&$variables) { } /** - * Implements hook_preprocess_HOOK() for field.tpl.php. - */ -function rdf_preprocess_field(&$variables) { - // Field formatters get the HTML data attributes as part of each field item - // in case they want to inject these attributes in their HTML output. - // If the field formatter didn't inject the HTML data attributes in its HTML - // and left them in the item, add these attributes to the field template. - foreach ($variables['element']['#items'] as $delta => $item) { - if (!empty($item['html_data_attributes'])) { - // Merge HTML data attributes with item_attributes. - $variables['item_attributes'][$delta] = new Attribute($item['html_data_attributes']); - } - } -} - -/** * Implements hook_preprocess_HOOK() for user.tpl.php. */ function rdf_preprocess_user(&$variables) { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php index 456582d..6579012 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php @@ -51,12 +51,12 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface '#options' => $uri['options'], ); - if (!empty($item->html_data_attributes)) { + if (!empty($item->_attributes)) { $elements[$delta]['#options'] += array('attributes' => array()); - $elements[$delta]['#options']['attributes'] += $item->html_data_attributes; - // Unset HTML data attributes since they've been added to the - // formatter and thus should not be rendered in the field template. - unset($item->html_data_attributes); + $elements[$delta]['#options']['attributes'] += $item->_attributes; + // Unset field item attributes since they have been included in the + // formatter output and should not be rendered in the field template. + unset($item->_attributes); } } }