summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-07-04 17:57:41 (GMT)
committerAlex Pott2014-07-04 17:57:41 (GMT)
commit227d641a976ac139bac0cd1b3b4204a9697dabb6 (patch)
treedd5f1a902992288b6e773e4ce559ec065222306d
parentf0cb85ac25e1c224aff7844c593ec36dd3d84888 (diff)
Issue #2188895 by lokapujya, seiplax | scor: Support RDFa output in entity reference field formatter.
-rw-r--r--core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php6
-rw-r--r--core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php12
-rw-r--r--core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php94
3 files changed, 110 insertions, 2 deletions
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 4065ea2..15f1df1 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 @@ class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {
// 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 992da6a..54a1ae7 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 @@ class EntityReferenceLabelFormatter extends EntityReferenceFormatterBase {
/** @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 0000000..891d605
--- /dev/null
+++ b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
@@ -0,0 +1,94 @@
+<?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'));
+ }
+
+}