diff --git a/core/includes/common.inc b/core/includes/common.inc index be2f2a4..5dec16c 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3468,6 +3468,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 25e2945..1d345cf 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 @@ -38,6 +38,9 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface '#title' => $item->value, '#href' => 'mailto:' . $item->value, ); + if (!empty($item->html_data_attributes)) { + $elements[$delta]['#attributes'] = $item->html_data_attributes; + } } return $elements; 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 2ef9df2..76e15d7 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 06b25a0..6f83646 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -197,6 +197,26 @@ function rdf_rdfa_attributes($mapping, $data = NULL) { */ /** + * Implements hook_entity_prepare_view(). + */ +function rdf_entity_prepare_view($entity_type, array $entities, array $displays, $view_mode) { + // Iterates over the RDF mappings for each entity and prepares the RDFa + // attributes to be added inside field formatters. + foreach ($entities as $id => $entity) { + $mapping = rdf_get_mapping($entity_type, $entity->bundle()); + // Only prepares the RDFa attributes for the fields which are configured + // to be displayed. + foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) { + $field_mapping = $mapping->getPreparedFieldMapping($name); + if ($field_mapping['properties']) { + $property = $entity->get($name); + $property->html_data_attributes = rdf_rdfa_attributes($field_mapping); + } + } + } +} + +/** * Implements hook_comment_load(). */ function rdf_comment_load($comments) { @@ -318,43 +338,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'] = entity_load('image_style', $element[$delta]['#image_style'])->buildUrl($item['entity']->getFileUri()); - } - else { - $variables['item_attributes'][$delta]['resource'] = file_create_url($item['entity']->getFileUri()); - } - } - // Convert the item_attributes for this field item back into an Attribute - // object for printing. This is necessary because at this time this - // preprocess function overwrites $variables['item_attributes'][$delta], - // which is initialized as an Attribute object in - // template_preprocess_field(). - $variables['item_attributes'][$delta] = new Attribute($variables['item_attributes'][$delta]); - } - } -} - -/** * Implements hook_preprocess_HOOK() for user.tpl.php. */ function rdf_preprocess_user(&$variables) { @@ -559,38 +542,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 536e6d2..1bc9d90 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 Drupal\user\UserInterface; @@ -194,6 +195,9 @@ function system_theme() { 'template' => 'system-plugin-ui-form', 'render element' => 'form', ), + 'html_data_wrapper' => array( + 'render element' => 'element', + ), )); } @@ -3319,6 +3323,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/TextDefaultFormatter.php b/core/modules/text/lib/Drupal/text/Plugin/field/formatter/TextDefaultFormatter.php index dd0ee71..2d77f0f 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,7 +42,19 @@ 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'); - $elements[$delta] = array('#markup' => $output); + if (empty($item->html_data_attributes)) { + $elements[$delta] = array('#markup' => $output); + } + else { + // Adds a wrapping element if attributes are specified for this item. + $elements[$delta] = array( + '#theme' => 'html_data_wrapper', + '#attributes' => $item->html_data_attributes, + 'children' => 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 2a95803..84575ee 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,9 +38,21 @@ public function viewElements(EntityInterface $entity, $langcode, FieldInterface $elements = array(); 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))); + 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 { + // Adds a wrapping element if attributes are specified for this item. + $elements[$delta] = array( + '#theme' => 'html_data_wrapper', + '#attributes' => $item->html_data_attributes, + 'children' => array( + '#markup' => nl2br(check_plain($item->value)), + ), + ); + } } return $elements;