Problem/Motivation

Similarly to #2034995: Test taxonomy term formatter RDFa output and #2188889: Support RDFa output in link field formatter, we need to support RDFa for the entity reference field formatter. The field formatter needs modification very similar to the taxonomy and link field formatters (see referenced issue).

Proposed resolution

Update the field formatter and add tests, see #2034995: Test taxonomy term formatter RDFa output and #2188889: Support RDFa output in link field formatter for examples.

The regular "label" formatter with link it is trivial, it's the same as the taxonomy link. For the "rendered entity" formatter, let's leave the property attribute in the field-item div HTML element, let's just add a @resource attribute with the value of entity in that same div. This will ensure that we're not dependent on the actual entity rendering HTML output to generate RDFa.

<div property="schema:reference" resource="node/1" class="field-item">

Remaining tasks

write patch.

User interface changes

none

API changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mlncn’s picture

Assigned: Unassigned » mlncn

Working on this but i cannot find a relation on Schema.org generic enough for an entity reference.

http://schema.org/mentions is the closest but that presupposes the referencing entity to be a creative work.

scor’s picture

It doesn't matter which schema.org property you use for the test, http://schema.org/mentions is fine, or you could also use http://schema.org/knows (say in the context of a social network built on Drupal).

scor’s picture

Issue summary: View changes
lokapujya’s picture

Assigned: mlncn » lokapujya
Status: Active » Needs review
FileSize
1.21 KB

Here is the label formatter portion of the patch. With this patch, the field formatter gives:
<a href="/node/1" property="schema:knows">First Article</a>

lokapujya’s picture

For the "rendered entity" formatter, we currently get this:

<?php
<div class="field-item" property="schema:knows">
?>

and we want:

<?php
<div class="field-item" property="schema:knows" resource="node/1" >
?>
scor’s picture

@lokapujya correct.

lokapujya’s picture

Here is the template: core/modules/system/templates/field.html.twig. It has

<?php
<div class="field-item"{{ item_attributes[delta] }}>{{ item }}</div>
?>

So, I think I need to modify item_attributes[delta].

I tried implementing the following function in: core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php

<?php
  public function view(FieldItemListInterface $items) {
    $options='';
    foreach ($items as $delta => $item) {
      $options['#options'] = array('resource'=>'node/' . $item->target_id);
    }
    return $options;
  }
?>

But, this is not working and there must be a better way.

scor’s picture

Status: Needs review » Needs work

@lokapujya there shouldn't be a need to implement a new method in EntityReferenceEntityFormatter, you should be able to support this in viewElements() right next to where we handle $item->attributes. In fact all that should be needed is to add that resource attribute to $item->_attributes, something like this:

$item->_attributes += array('resource' => 'node/1');
lokapujya’s picture

Status: Needs work » Needs review
FileSize
2.1 KB
908 bytes

Wow! I tried something like that originally and it didn't work. Now, it works; So, I was just doing something wrong.

lokapujya’s picture

I will need to change the code to support other entity types. I shouldn't have hard coded node/.

scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php
@@ -101,6 +101,7 @@ public function viewElements(FieldItemListInterface $items) {
+        $item->_attributes += array('resource' => 'node/' . $item->target_id);

First, check if $item->_attributes is not empty, since we don't need to do anything unless it has a property attribute. Then, just use $entity->url(). Don't forget to add a comment similar to this: 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. Add a resource attribute to explicitly set the url of the entity as the value for this mapping property.

lokapujya’s picture

made changes from #11.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
2.38 KB

Attach patch this time.

lokapujya’s picture

FileSize
5.59 KB

Adding a test.

Status: Needs review » Needs work

The last submitted patch, 14: 2188895-14.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
1.67 KB
5.58 KB

Trying to get this to work. Here are some work in progress.

Status: Needs review » Needs work

The last submitted patch, 16: 2188895-16.patch, failed testing.

lokapujya’s picture

FileSize
5.49 KB

renaamed entity2 to target entity.

scor’s picture

+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/EntityReferenceRdfaTest.php
@@ -0,0 +1,95 @@
+  /**
+   * The term for testing.
+   *
+   * @var \Drupal\taxonomy\Entity\Term
+   */
+  protected $target_entity;
+

This should be more accurate: The target entity for testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.65 KB

some progress.
currently, line 85 in assertFormatterRdfa():
$output = drupal_render($build);
is not returning the markup that I expect. It's giving 'default | vU5eSA5L', where I expect

<?php
<a href="/node/1" property="schema:knows">First Article</a>
?>

Status: Needs review » Needs work

The last submitted patch, 20: 2188895-19.patch, failed testing.

scor’s picture

I tried with:

    entity_create('field_instance_config', array(
      'entity_type' => 'entity_test',
      'field_name' => $this->fieldName,
      'bundle' => 'entity_test',
      'settings' => array(
        'handler' => 'default',
        'handler_settings' => array(
          'target_bundles' => array('entity_test' => 'entity_test'),
        ),
      ),
    ))->save();

but it didn't make any difference to the output of the entity_reference_label or entity_reference_entity_id formatters.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
1.72 KB
5.81 KB

Yes, I am also getting the exact same output for both the label and entity view test.

'default | pEsi6pbw<div class="field field-entity-test--name field-name-name field-type-string field-label-hidden">
    <div class="field-items">
          <div class="field-item">pEsi6pbw</div>
      </div>
</div>
'

Status: Needs review » Needs work

The last submitted patch, 23: 2188895-23.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
8.32 KB
5.22 KB

1. The test is now creating markup like this for the label formatter:

'default | cYzXsyaV<div class="field field-entity-test--name field-name-name field-type-string field-label-hidden">
    <div class="field-items">
          <div class="field-item">cYzXsyaV</div>
      </div>
</div>
<div class="field field-entity-test--field-test field-name-field-test field-type-entity-reference field-label-above">
      <div class="field-label">Field test:&nbsp;</div>
    <div class="field-items">
          <div class="field-item"><a href="/entity_test/1" property="schema:knows">hawjmEBq</a></div>
      </div>
</div>
'

2. I fixed a bug that I had put into the EntityReferenceEntityFormatter in an earlier patch.

3. I'm not sure why I needed to add a getRelativeUri() since other tests use getAbsoluteUri().

4. The expected value for the entity formatter might still need to be adjusted.

scor’s picture

Wow! Great job Jamie! It's passing the tests :) I will review by our next sprint on Tue!

scor’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
    
    +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemListInterface.php
    

    Looks like these files are irrelevant here.

  2. +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php
    @@ -114,6 +114,12 @@ public function viewElements(FieldItemListInterface $items) {
    +        // entity's url. Since we don't know what the markup of the entity will
    +        // be, we shouldn't rely on the markup for structured data such as RDFa.
    

    we shouldn't rely on *it* for structured data.

  3. +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/Field/FieldRdfaTestBase.php
    @@ -115,5 +115,8 @@ protected function createTestField() {
    diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
    
    diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
    old mode 100644
    new mode 100755
     
    

    that file shouldn't be part of the patch.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
11.45 KB

Progress from code sprint.

Status: Needs review » Needs work

The last submitted patch, 28: 2188895-28.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
6 KB

Please ignore the last patch.

scor’s picture

Status: Needs review » Needs work

This needs to be rerolled for PSR-4

seiplax’s picture

Status: Needs work » Needs review
FileSize
2.76 KB

re-rolled after PSR-4

scor’s picture

Status: Needs review » Needs work

The last submitted patch, 32: support-rdfa-output-in-entity-reference-2188895-32.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
2.76 KB

Just a reroll.

scor’s picture

Status: Needs review » Needs work

looks like we're missing the test which was present in #30.

The last submitted patch, 35: 2188895-35.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.74 KB

Added the test back in.

Status: Needs review » Needs work

The last submitted patch, 38: 2188895-38.patch, failed testing.

scor’s picture

looks like #2183231: Make ContentEntityDatabaseStorage generate static database schemas for content entities introduced a change? either way, the test is passing if I use

    $this->installEntitySchema('entity_test_rev');

instead of

    $this->installSchema('entity_test', array('entity_test_rev', 'entity_test_rev_revision'));
lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.98 KB
1.95 KB

In this patch, I am back to using getAbsoluteUri(). Needed to set $this->uri to the expected uri. Also, I fixed the issue in the previous comment.

scor’s picture

Status: Needs review » Needs work

Looks great, glad you found the culprit that was causing the URI issue.

+++ b/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php
@@ -120,7 +120,7 @@ public function getId()
-            throw new \LogicException('Cannot change the ID of an active session');
+            //throw new \LogicException('Cannot change the ID of an active session');

we should remove that :)

lokapujya’s picture

I was trying to sneak it in, haha.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.17 KB

Removed the hack.

scor’s picture

Status: Needs review » Needs work

This looks good to go, except for this minor comment:

+++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php
@@ -76,6 +76,14 @@ public function viewElements(FieldItemListInterface $items) {
+            // Unset field item attributes since they have been included in the
+            // formatter output and should not be rendered in the field template.

This won't pass coding standards, it's > 80 chars. compacting should not to shouldn't should make it fit on the same line...

lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.65 KB

Fixed some comment spacing.

lokapujya’s picture

FileSize
1.55 KB

attaching the interdiff for last patch.

scor’s picture

Status: Needs review » Reviewed & tested by the community

Thanks Jamie!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 227d641 and pushed to 8.x. Thanks!

  • alexpott committed 227d641 on 8.x
    Issue #2188895 by lokapujya, seiplax | scor: Support RDFa output in...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.