From 6b0b4cfc417a6ab82959da2cd7ef5f6b973b1058 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Mon, 10 Mar 2014 13:19:34 +0000 Subject: [PATCH] Issue #2139551 by djevans, cwells, ashepherd, vijaycs85: Support RDFa output in date formatter. --- .../DateTimeDefaultFormatter.php | 9 ++- .../rdf/Tests/Field/DateTimeFieldRdfaTest.php | 63 +++++++++++++++++++ .../rdf/Tests/Field/FieldRdfaTestBase.php | 8 ++- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 core/modules/rdf/lib/Drupal/rdf/Tests/Field/DateTimeFieldRdfaTest.php diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php index 1a235dca7f90..754ffc0aaa87 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php @@ -116,17 +116,20 @@ public function viewElements(FieldItemListInterface $items) { } // Display the date using theme datetime. - // @todo How should RDFa attributes be added to this? $elements[$delta] = array( '#theme' => 'datetime', '#text' => $formatted_date, '#html' => FALSE, '#attributes' => array( 'datetime' => $iso_date, - 'property' => array('dc:date'), - 'datatype' => 'xsd:dateTime', ), ); + if (!empty($item->_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); + } } return $elements; diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/DateTimeFieldRdfaTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/DateTimeFieldRdfaTest.php new file mode 100644 index 000000000000..d859f25f5f2e --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/DateTimeFieldRdfaTest.php @@ -0,0 +1,63 @@ +<?php +/** + * @file + * Contains \Drupal\rdf\Tests\Field\DateTimeFieldRdfaTest. + */ + +namespace Drupal\rdf\Tests\Field; + +use Drupal\rdf\Tests\Field\FieldRdfaTestBase; + +/** + * Tests the placement of RDFa in text field formatters. + */ +class DateTimeFieldRdfaTest extends FieldRdfaTestBase { + + /** + * {@inheritdoc} + */ + protected $fieldType = 'datetime'; + + /** + * The 'value' property value for testing. + * + * @var string + */ + protected $testValue = '2014-01-28T06:01:01'; + + /** + * {@inheritdoc} + */ + public static $modules = array('datetime'); + + public static function getInfo() { + return array( + 'name' => 'Field formatter: datetime', + 'description' => 'Tests RDFa output by datetime 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:dateCreated'), + ))->save(); + + // Set up test entity. + $this->entity = entity_create('entity_test', array()); + $this->entity->{$this->fieldName}->value = $this->testValue; + } + + /** + * Tests the default formatter. + */ + public function testDefaultFormatter() { + $this->assertFormatterRdfa('datetime_default', 'http://schema.org/dateCreated', $this->testValue . 'Z', 'literal', 'http://www.w3.org/2001/XMLSchema#dateTime'); + } +} 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 9a9dec2f7209..5afeeab4bca2 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php @@ -66,8 +66,10 @@ public function setUp() { * The expected value of the property. * @param string $object_type * The object's type, either 'uri' or 'literal'. + * @param string $datatype + * The data type of the property. */ - protected function assertFormatterRdfa($formatter, $property, $value, $object_type = 'literal') { + protected function assertFormatterRdfa($formatter, $property, $value, $object_type = 'literal', $datatype = '') { // The field formatter will be rendered inside the entity. Set the field // formatter in the entity display options before rendering the entity. entity_get_display('entity_test', 'entity_test', 'default') @@ -76,11 +78,13 @@ protected function assertFormatterRdfa($formatter, $property, $value, $object_ty $build = entity_view($this->entity, 'default'); $output = drupal_render($build); $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa'); - $expected_value = array( 'type' => $object_type, 'value' => $value, ); + if ($datatype) { + $expected_value['datatype'] = $datatype; + } $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_value), "Formatter $formatter exposes data correctly for {$this->fieldType} fields."); } -- GitLab