Commit 6b0b4cfc authored by catch's avatar catch

Issue #2139551 by djevans, cwells, ashepherd, vijaycs85: Support RDFa output in date formatter.

parent 6dc4d08c
...@@ -116,17 +116,20 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -116,17 +116,20 @@ public function viewElements(FieldItemListInterface $items) {
} }
// Display the date using theme datetime. // Display the date using theme datetime.
// @todo How should RDFa attributes be added to this?
$elements[$delta] = array( $elements[$delta] = array(
'#theme' => 'datetime', '#theme' => 'datetime',
'#text' => $formatted_date, '#text' => $formatted_date,
'#html' => FALSE, '#html' => FALSE,
'#attributes' => array( '#attributes' => array(
'datetime' => $iso_date, '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; return $elements;
......
<?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');
}
}
...@@ -66,8 +66,10 @@ public function setUp() { ...@@ -66,8 +66,10 @@ public function setUp() {
* The expected value of the property. * The expected value of the property.
* @param string $object_type * @param string $object_type
* The object's type, either 'uri' or 'literal'. * 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 // The field formatter will be rendered inside the entity. Set the field
// formatter in the entity display options before rendering the entity. // formatter in the entity display options before rendering the entity.
entity_get_display('entity_test', 'entity_test', 'default') entity_get_display('entity_test', 'entity_test', 'default')
...@@ -76,11 +78,13 @@ protected function assertFormatterRdfa($formatter, $property, $value, $object_ty ...@@ -76,11 +78,13 @@ protected function assertFormatterRdfa($formatter, $property, $value, $object_ty
$build = entity_view($this->entity, 'default'); $build = entity_view($this->entity, 'default');
$output = drupal_render($build); $output = drupal_render($build);
$graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa'); $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa');
$expected_value = array( $expected_value = array(
'type' => $object_type, 'type' => $object_type,
'value' => $value, '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."); $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_value), "Formatter $formatter exposes data correctly for {$this->fieldType} fields.");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment