diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index 4065ea2204609871e5f1496e5933047297a18da0..15f1df117f4462d1516f1d1c6981d762f7967668 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -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. diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php index 992da6a5e2e23537e6d429dbc50c6210a7a961cd..54a1ae786796f52a155ec995f649d0b56866b13e 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php @@ -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)); diff --git a/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php new file mode 100644 index 0000000000000000000000000000000000000000..891d60533d9edd9eefb5020fc7ad63843e694985 --- /dev/null +++ b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php @@ -0,0 +1,94 @@ + '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')); + } + +}