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/EmailFieldRdfaTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/EmailFieldRdfaTest.php new file mode 100644 index 0000000..0cf5723 --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/EmailFieldRdfaTest.php @@ -0,0 +1,64 @@ + 'Field formatter - email', + 'description' => 'Tests RDFa output by email field formatters.', + 'group' => 'RDF', + ); + } + + public function setUp() { + parent::setUp(); + + $this->createTestField(); + + // Add the mapping. + $mapping = rdf_get_mapping('entity_test', 'entity_test'); + $mapping->setFieldMapping($this->fieldName, array( + 'properties' => array('schema:email'), + ))->save(); + + // Set up test values. + $this->testValue = 'test@example.com'; + $this->entity = entity_create('entity_test', array()); + $this->entity->{$this->fieldName}->value = $this->testValue; + $this->entity->save(); + $uri_info = $this->entity->uri(); + $this->uri = url($uri_info['path']); + } + + /** + * Tests the plain formatter. + */ + public function testPlainFormatter() { + $this->_testFormatter('text_plain', 'http://schema.org/email', $this->testValue); + } + + /** + * Tests the mailto formatter. + */ + public function testMailToFormatter() { + $this->_testFormatter('email_mailto', 'http://schema.org/email', $this->testValue); + } +} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php new file mode 100644 index 0000000..1404216 --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php @@ -0,0 +1,100 @@ +entity, $this->fieldName, array('type' => $formatter)); + $rendered = drupal_render($build); + $graph = new \EasyRdf_Graph($this->uri, $rendered, 'rdfa'); + + $expected_value = array( + 'type' => $object_type, + 'value' => $value, + ); + $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_value), "Formatter $formatter exposes data correctly for {$this->fieldType} fields."); + } + + /** + * Creates the field for testing. + */ + protected function createTestField() { + entity_create('field_entity', array( + 'field_name' => $this->fieldName, + 'type' => $this->fieldType, + ))->save(); + entity_create('field_instance', array( + 'entity_type' => 'entity_test', + 'field_name' => $this->fieldName, + 'bundle' => 'entity_test', + ))->save(); + } + + /** + * Gets the absolute URI of an entity. + * + * @param \Drupal\Core\Entity\EntityNG $entity + * The entity for which to generate the URI. + * + * @return string + * The absolute URI. + */ + protected function getAbsoluteUri($entity) { + $uri_info = $entity->uri(); + return url($uri_info['path'], array('absolute' => TRUE)); + } +} 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/system/lib/Drupal/system/Tests/Theme/FunctionsUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/FunctionsUnitTest.php new file mode 100644 index 0000000..c6300c4 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/FunctionsUnitTest.php @@ -0,0 +1,42 @@ + 'Theme functions unit test', + 'description' => 'Tests common theme functions.', + 'group' => 'Theme', + ); + } + + /** + * Test the HTML data wrapper. + */ + public function testHtmlDataWrapper() { + $element = array( + '#theme' => 'html_data_wrapper', + '#attributes' => array('itemprop' => 'http://schema.org/name'), + 'children' => array('#markup' => 'test name'), + ); + + $rendered = drupal_render($element); + $expected = 'test name'; + $this->assertEqual($rendered, $expected, 'theme_html_data_wrapper produces expected output'); + } +} diff --git a/core/modules/system/system.module b/core/modules/system/system.module index a6a7786..3ab983c 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Language\Language; +use Drupal\Core\Template\Attribute; use Drupal\Core\Utility\ModuleInfo; use Drupal\system\Plugin\Block\SystemMenuBlock; use Symfony\Component\HttpFoundation\JsonResponse; @@ -193,6 +194,9 @@ function system_theme() { 'template' => 'system-plugin-ui-form', 'render element' => 'form', ), + 'html_data_wrapper' => array( + 'render element' => 'element', + ), )); } @@ -3596,6 +3600,12 @@ function theme_exposed_filters($variables) { return '
' . $output . '
'; } +function theme_html_data_wrapper($variables) { + $element = $variables['element']; + $attributes = new Attribute($element['#attributes']); + return "" . drupal_render_children($element) . ""; +} + /** * Implements hook_admin_paths(). */ 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;