diff --git a/core/includes/common.inc b/core/includes/common.inc
index 3852870..b389530 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -3482,6 +3482,13 @@ function drupal_pre_render_html_tag($element) {
function drupal_pre_render_link($element) {
// By default, link options to pass to l() are normally set in #options.
$element += array('#options' => array());
+
+ // The title might be a render array. Render it to pass a string to l().
+ if (is_array($element['#title'])) {
+ $element['#title'] = drupal_render($element['#title']);
+ $element['#options']['html'] = TRUE;
+ }
+
// However, within the scope of renderable elements, #attributes is a valid
// way to specify attributes, too. Take them into account, but do not override
// attributes from #options.
diff --git a/core/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php b/core/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
index 33e5920..d9935fb 100644
--- a/core/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
+++ b/core/modules/email/lib/Drupal/email/Plugin/field/formatter/MailToFormatter.php
@@ -33,9 +33,16 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
foreach ($items as $delta => $item) {
+ $title = array(
+ '#theme' => 'html_data_wrapper',
+ '#attributes' => $item['html_data_attributes'],
+ 'children' => array(
+ '#markup' => $item['value'],
+ ),
+ );
$elements[$delta] = array(
'#type' => 'link',
- '#title' => $item['value'],
+ '#title' => $title,
'#href' => 'mailto:' . $item['value'],
);
}
diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php
index fa780a5..ab01ade 100644
--- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php
+++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php
@@ -120,7 +120,9 @@ public function settingsSummary() {
/**
* {@inheritdoc}
*/
- public function prepareView(array $entities, $langcode, array &$items) { }
+ public function prepareView(array $entities, $langcode, array &$items) {
+ \Drupal::moduleHandler()->invokeAll('formatter_prepare_view', array($entities, $langcode, &$items, $this->fieldDefinition));
+ }
/**
* Returns whether the currently logged in user has access to the field.
diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php
index ccf28d3..9c72459 100644
--- a/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php
+++ b/core/modules/file/lib/Drupal/file/Plugin/field/formatter/FileFormatterBase.php
@@ -18,6 +18,8 @@
* {@inheritdoc}
*/
public function prepareView(array $entities, $langcode, array &$items) {
+ parent::prepareView($entities, $langcode, $items);
+
// Remove files specified to not be displayed.
$fids = array();
foreach ($entities as $id => $entity) {
diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php
index 3445c42..d0e59a7 100644
--- a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php
@@ -29,12 +29,12 @@
*
* @var string
*/
- protected $uri = 'http://ex.com';
+ protected $uri;
/**
* The entity to render for testing.
*
- * @var \Drupal\Core\Entity\EntityNG
+ * @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
@@ -59,7 +59,7 @@
*/
protected function _testFormatter($formatter, $property, $value, $object_type = 'literal') {
$build = field_view_field($this->entity, $this->fieldName, array('type' => $formatter));
- $rendered = "
" . drupal_render($build) . '
';
+ $rendered = drupal_render($build);
$graph = new \EasyRdf_Graph($this->uri, $rendered, 'rdfa');
$expected_value = array(
@@ -87,7 +87,7 @@ protected function createTestField() {
/**
* Gets the absolute URI of an entity.
*
- * @param \Drupal\Core\Entity\EntityNG $entity
+ * @param \Drupal\Core\Entity\EntityInterface $entity
* The entity for which to generate the URI.
*
* @return string
diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/TaxonomyTermReferenceRdfaTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/TaxonomyTermReferenceRdfaTest.php
index 748d279..9ac3073 100644
--- a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/TaxonomyTermReferenceRdfaTest.php
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/TaxonomyTermReferenceRdfaTest.php
@@ -84,19 +84,28 @@ public function setUp() {
$mapping = rdf_get_mapping('entity_test', 'entity_test');
$mapping->setFieldMapping($this->fieldName, array(
'properties' => array('schema:about'),
- 'mapping_type' => 'rel',
))->save();
// Set up test values.
$this->entity = entity_create('entity_test', array());
$this->entity->get($this->fieldName)->offsetGet(0)->get('target_id')->setValue($this->term->id());
+ $this->entity->save();
+ $this->uri = $this->getAbsoluteUri($this->entity);
+ }
+
+ /**
+ * Tests the plain formatter.
+ */
+ public function testPlainFormatter() {
+ $term_uri = $this->getAbsoluteUri($this->term);
+ $this->_testFormatter('taxonomy_term_reference_plain', 'http://schema.org/about', $term_uri, 'uri');
}
/**
* Test the link formatter.
*/
public function testLinkFormatter() {
- $term_uri = 'http://ex.com/taxonomy/term/1';
+ $term_uri = $this->getAbsoluteUri($this->term);
$this->_testFormatter('taxonomy_term_reference_link', 'http://schema.org/about', $term_uri, 'uri');
}
}
diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module
index 3c03d80..8887a24 100644
--- a/core/modules/rdf/rdf.module
+++ b/core/modules/rdf/rdf.module
@@ -221,6 +221,33 @@ function rdf_theme() {
}
/**
+ * Implements hook_formatter_prepare_view().
+ */
+function rdf_formatter_prepare_view($entities, $langcode, &$items, $field_definition) {
+ // Get the field mapping for this field.
+ $field = $field_definition['field'];
+ $field_name = $field->getFieldName();
+ $mapping = rdf_get_mapping($field_definition['entity_type'], $field_definition['bundle']);
+ $field_mapping = $mapping->getPreparedFieldMapping($field_name);
+
+ // Attach the prepared RDF attributes to the item. These attributes will be
+ // placed in the field formatter.
+ foreach ($items as $entity_id => &$item_list) {
+ $entity = $entities[$entity_id];
+ $uri_info = $entities[$entity_id]->uri();
+ foreach ($item_list as $delta => &$item) {
+ $value = $entity->get($field_name)->offsetGet($delta)->getValue();
+ $item['html_data_attributes'] = rdf_rdfa_attributes($field_mapping, $value);
+ // Add an about attribute to create the relationship between the field
+ // and its entity. This ensures that data is properly exposed when using
+ // layout tools like Views and that other changes to the formatter do not
+ // impact the data (for example, wrapping the field in a link).
+ $item['html_data_attributes']['about'] = url($uri_info['path']);
+ }
+ }
+}
+
+/**
* Implements hook_preprocess_HOOK() for html.tpl.php.
*/
function rdf_preprocess_html(&$variables) {
@@ -324,37 +351,6 @@ function rdf_preprocess_node(&$variables) {
}
/**
- * Implements hook_preprocess_HOOK() for field.tpl.php.
- */
-function rdf_preprocess_field(&$variables) {
- $element = $variables['element'];
- $entity_type = $element['#entity_type'];
- $bundle = $element['#bundle'];
- $field_name = $element['#field_name'];
- $field_mapping = rdf_get_mapping($entity_type, $bundle)
- ->getPreparedFieldMapping($field_name);
-
- if (!empty($field_mapping)) {
- foreach ($element['#items'] as $delta => $item) {
- $variables['item_attributes'][$delta] = rdf_rdfa_attributes($field_mapping, $item);
- // If this field is an image, RDFa will not output correctly when the
- // image is in a containing tag. If the field is a file, RDFa will
- // not output correctly if the filetype icon comes before the link to the
- // file. We correct this by adding a resource attribute to the div if
- // this field has a URI.
- if (isset($item['entity']->uri)) {
- if (!empty($element[$delta]['#image_style'])) {
- $variables['item_attributes'][$delta]['resource'] = image_style_url($element[$delta]['#image_style'], $item['entity']->getFileUri());
- }
- else {
- $variables['item_attributes'][$delta]['resource'] = file_create_url($item['entity']->getFileUri());
- }
- }
- }
- }
-}
-
-/**
* Implements hook_preprocess_HOOK() for user.tpl.php.
*/
function rdf_preprocess_user(&$variables) {
@@ -559,38 +555,6 @@ function rdf_preprocess_taxonomy_term(&$variables) {
}
/**
- * Implements hook_field_attach_view_alter().
- */
-function rdf_field_attach_view_alter(&$output, $context) {
- // Append term mappings on displayed taxonomy links.
- foreach (element_children($output) as $field_name) {
- $element = &$output[$field_name];
- if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
- foreach ($element['#items'] as $delta => $item) {
- // This function is invoked during entity preview when taxonomy term
- // reference items might contain free-tagging terms that do not exist
- // yet and thus have no $item['entity'].
- if (isset($item['entity'])) {
- $term = $item['entity'];
- $mapping = rdf_get_mapping('taxonomy_term', $term->bundle());
- $bundle_mapping = $mapping->getPreparedBundleMapping();
- if (!empty($bundle_mapping['types'])) {
- $element[$delta]['#options']['attributes']['typeof'] = $bundle_mapping['types'];
- }
- $name_field_mapping = $mapping->getPreparedFieldMapping('name');
- if (!empty($name_field_mapping['properties'])) {
- // A property attribute is used with an empty datatype attribute so
- // the term name is parsed as a plain literal in RDFa 1.0 and 1.1.
- $element[$delta]['#options']['attributes']['property'] = $name_field_mapping['properties'];
- $element[$delta]['#options']['attributes']['datatype'] = '';
- }
- }
- }
- }
- }
-}
-
-/**
* Implements hook_preprocess_HOOK() for theme_image().
*/
function rdf_preprocess_image(&$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 b44002e..51ebdd7 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
@@ -50,6 +50,13 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
'#href' => $uri['path'],
'#options' => $uri['options'],
);
+
+ if (!empty($item['html_data_attributes'])) {
+ if (!isset($elements[$delta]['#options']['attributes'])) {
+ $elements[$delta]['#options']['attributes'] = array();
+ }
+ $elements[$delta]['#options']['attributes'] += $item['html_data_attributes'];
+ }
}
}
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php
index 90f76ad..00e5cc3 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php
@@ -24,6 +24,8 @@
* unsets values for invalid terms that do not exist.
*/
public function prepareView(array $entities, $langcode, array &$items) {
+ parent::prepareView($entities, $langcode, $items);
+
$tids = array();
// Collect every possible term attached to any of the fieldable entities.
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 813bb92..7971f64 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
@@ -40,7 +40,13 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
foreach ($items as $delta => $item) {
// 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'])));
+ $elements[$delta] = array(
+ '#theme' => 'html_data_wrapper',
+ '#attributes' => $item['html_data_attributes'],
+ 'children' => array(
+ '#markup' => nl2br(check_plain($item['value'])),
+ ),
+ );
}
return $elements;