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 0a7a556..f727b85 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 @@ -41,6 +41,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface // Pass HTML data attributes to the theme function. if (isset($item->html_data_attributes)) { $elements[$delta]['#attributes'] = $item->html_data_attributes; + // Unset HTML data attributes since they've been rendered in the + // formatter and do not need to be rendered in the field template. + unset($item->html_data_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 9585d71..144f005 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 @@ -124,6 +124,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface if (isset($item->html_data_attributes)) { $elements[$delta]['#item'] += array('attributes' => array()); $elements[$delta]['#item']['attributes'] += $item->html_data_attributes; + // Unset HTML data attributes since they've been rendered in the + // formatter and do not need to be rendered in the field template. + unset($item->html_data_attributes); } } } diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 61756ff..8ca5e6f 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -339,6 +339,21 @@ function rdf_preprocess_node(&$variables) { } /** + * Implements hook_preprocess_HOOK() for field.tpl.php. + */ +function rdf_preprocess_field(&$variables) { + // If the field formatter didn't inject the HTML data attributes in its HTML + // output, add these attributes to the field template. + foreach ($variables['element']['#items'] as $delta => $item) { + if (!empty($item['html_data_attributes'])) { + // Merge HTML data attributes in item_attributes. + // @todo https://drupal.org/node/2034003 + $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 62ad9a9..b669c7d 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 @@ -54,6 +54,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface if (!empty($item->html_data_attributes)) { $elements[$delta]['#options'] += array('attributes' => array()); $elements[$delta]['#options']['attributes'] += $item->html_data_attributes; + // Unset HTML data attributes since they've been rendered in the + // formatter and do not need to be rendered in the field template. + unset($item->html_data_attributes); } } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php index 03312e5..36de426 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php @@ -33,21 +33,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $elements = array(); foreach ($items as $delta => $item) { - $output = check_plain($item->entity->label()); - if (empty($item->html_data_attributes)) { - $elements[$delta] = array( - '#markup' => $output, - ); - } - else { - // Add a wrapping element if attributes are specified for this item. - $elements[$delta] = array( - '#type' => 'html_tag', - '#tag' => 'span', - '#attributes' => $item->html_data_attributes, - '#value' => $output, - ); - } + $elements[$delta] = array( + '#markup' => check_plain($item->entity->label()), + ); } return $elements; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php index 1c6f320..dd0ee71 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php @@ -42,18 +42,7 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface // https://drupal.org/node/2026339. $itemBC = $item->getValue(TRUE); $output = text_sanitize($this->getFieldSetting('text_processing'), $langcode, $itemBC, 'value'); - if (empty($item->html_data_attributes)) { - $elements[$delta] = array('#markup' => $output); - } - else { - // Add a wrapping element if attributes are specified for this item. - $elements[$delta] = array( - '#type' => 'html_tag', - '#tag' => 'div', - '#attributes' => $item->html_data_attributes, - '#value' => $output, - ); - } + $elements[$delta] = array('#markup' => $output); } return $elements; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextPlainFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextPlainFormatter.php index e517cc0..2a95803 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextPlainFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextPlainFormatter.php @@ -38,20 +38,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $elements = array(); foreach ($items as $delta => $item) { - if (empty($item->html_data_attributes)) { - // The text value has no text format assigned to it, so the user input - // should equal the output, including newlines. - $elements[$delta] = array('#markup' => nl2br(check_plain($item->value))); - } - else { - // Add a wrapping element if attributes are specified for this item. - $elements[$delta] = array( - '#type' => 'html_tag', - '#tag' => 'span', - '#attributes' => $item->html_data_attributes, - '#value' => nl2br(check_plain($item->value)), - ); - } + // The text value has no text format assigned to it, so the user input + // should equal the output, including newlines. + $elements[$delta] = array('#markup' => nl2br(check_plain($item->value))); } return $elements; diff --git a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php index 1a44607..f2ca9e4 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php +++ b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextTrimmedFormatter.php @@ -77,18 +77,7 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $output = text_sanitize($text_processing, $langcode, $item->getValue(TRUE), 'value'); $output = text_summary($output, $text_processing ? $item->format : NULL, $this->getSetting('trim_length')); } - if (empty($item->html_data_attributes)) { - $elements[$delta] = array('#markup' => $output); - } - else { - // Add a wrapping element if attributes are specified for this item. - $elements[$delta] = array( - '#type' => 'html_tag', - '#tag' => 'div', - '#attributes' => $item->html_data_attributes, - '#value' => $output, - ); - } + $elements[$delta] = array('#markup' => $output); } return $elements;