Commit 227d641a authored by alexpott's avatar alexpott

Issue #2188895 by lokapujya, seiplax | scor: Support RDFa output in entity...

Issue #2188895 by lokapujya, seiplax | scor: Support RDFa output in entity reference field formatter.
parent f0cb85ac
......@@ -115,6 +115,12 @@ public function viewElements(FieldItemListInterface $items) {
// Hide the element links.
$elements[$delta][$target_type][$item->target_id]['links']['#access'] = FALSE;
}
// Add a resource attribute to set the mapping property's value to the
// entity's url. Since we don't know what the markup of the entity will
// be, we shouldn't rely on it for structured data such as RDFa.
if (!empty($item->_attributes)) {
$item->_attributes += array('resource' => $item->entity->url());
}
}
else {
// This is an "auto_create" item.
......
......@@ -69,13 +69,21 @@ public function viewElements(FieldItemListInterface $items) {
/** @var $referenced_entity \Drupal\Core\Entity\EntityInterface */
if ($referenced_entity = $item->entity) {
$label = $referenced_entity->label();
// If the link is to be displayed and the entity has a uri,
// display a link.
// If the link is to be displayed and the entity has a uri, display a
// link.
if ($this->getSetting('link') && $uri = $referenced_entity->urlInfo()) {
$elements[$delta] = array(
'#type' => 'link',
'#title' => $label,
) + $uri->toRenderArray();
if (!empty($item->_attributes)) {
$elements[$delta]['#options'] += array('attributes' => array());
$elements[$delta]['#options']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and shouldn't be rendered in the field template.
unset($item->_attributes);
}
}
else {
$elements[$delta] = array('#markup' => String::checkPlain($label));
......
<?php
/**
* @file
* Contains \Drupal\rdf\Tests\Field\TaxonomyTermReferenceRdfaTest.
*/
namespace Drupal\rdf\Tests\Field;
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Language\Language;
/**
* Tests the RDFa output of the entity reference field formatter.
*/
class EntityReferenceRdfaTest extends FieldRdfaTestBase {
/**
* {@inheritdoc}
*/
protected $fieldType = 'entity_reference';
/**
* The entity type used in this test.
*
* @var string
*/
protected $entityType = 'entity_test';
/**
* The bundle used in this test.
*
* @var string
*/
protected $bundle = 'entity_test';
/**
* The term for testing.
*
* @var \Drupal\taxonomy\Entity\Term
*/
protected $target_entity;
/**
* {@inheritdoc}
*/
public static $modules = array('entity', 'entity_reference', 'options', 'text', 'filter');
public static function getInfo() {
return array(
'name' => 'Field formatter: entity reference',
'description' => 'Tests RDFa output by entity reference field formatters.',
'group' => 'RDF',
);
}
public function setUp() {
parent::setUp();
$this->installEntitySchema('entity_test_rev');
entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
// Add the mapping.
$mapping = rdf_get_mapping('entity_test', 'entity_test');
$mapping->setFieldMapping($this->fieldName, array(
'properties' => array('schema:knows'),
))->save();
// Create the entity to be referenced.
$this->target_entity = entity_create($this->entityType, array('name' => $this->randomName()));
$this->target_entity->save();
// Create the entity that will have the entity reference field.
$this->entity = entity_create($this->entityType, array('name' => $this->randomName()));
$this->entity->save();
$this->entity->{$this->fieldName}->entity = $this->target_entity;
$this->entity->{$this->fieldName}->access = TRUE;
$this->uri = $this->getAbsoluteUri($this->entity);
}
/**
* Tests all the entity reference formatters.
*/
public function testAllFormatters() {
$entity_uri = $this->getAbsoluteUri($this->target_entity);
// Tests the label formatter.
$this->assertFormatterRdfa(array('type' => 'entity_reference_label'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
// Tests the entity formatter.
$this->assertFormatterRdfa(array('type' => 'entity_reference_entity_view'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
}
}
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