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
Comment | File | Size | Author |
---|---|---|---|
#47 | interdiff.txt | 1.55 KB | lokapujya |
#46 | 2188895-46.patch | 5.65 KB | lokapujya |
Comments
Comment #1
mlncn CreditAttribution: mlncn commentedWorking 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.
Comment #2
scor CreditAttribution: scor commentedIt 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).
Comment #3
scor CreditAttribution: scor commentedComment #4
lokapujyaHere 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>
Comment #5
lokapujyaFor the "rendered entity" formatter, we currently get this:
and we want:
Comment #6
scor CreditAttribution: scor commented@lokapujya correct.
Comment #7
lokapujyaHere is the template: core/modules/system/templates/field.html.twig. It has
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
But, this is not working and there must be a better way.
Comment #8
scor CreditAttribution: scor commented@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:
Comment #9
lokapujyaWow! I tried something like that originally and it didn't work. Now, it works; So, I was just doing something wrong.
Comment #10
lokapujyaI will need to change the code to support other entity types. I shouldn't have hard coded node/.
Comment #11
scor CreditAttribution: scor commentedFirst, 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.
Comment #12
lokapujyamade changes from #11.
Comment #13
lokapujyaAttach patch this time.
Comment #14
lokapujyaAdding a test.
Comment #16
lokapujyaTrying to get this to work. Here are some work in progress.
Comment #18
lokapujyarenaamed entity2 to target entity.
Comment #19
scor CreditAttribution: scor commentedThis should be more accurate: The target entity for testing.
Comment #20
lokapujyasome 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
Comment #22
scor CreditAttribution: scor commentedI tried with:
but it didn't make any difference to the output of the entity_reference_label or entity_reference_entity_id formatters.
Comment #23
lokapujyaYes, I am also getting the exact same output for both the label and entity view test.
Comment #25
lokapujya1. The test is now creating markup like this for the label formatter:
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.
Comment #26
scor CreditAttribution: scor commentedWow! Great job Jamie! It's passing the tests :) I will review by our next sprint on Tue!
Comment #27
scor CreditAttribution: scor commentedLooks like these files are irrelevant here.
we shouldn't rely on *it* for structured data.
that file shouldn't be part of the patch.
Comment #28
lokapujyaProgress from code sprint.
Comment #30
lokapujyaPlease ignore the last patch.
Comment #31
scor CreditAttribution: scor commentedThis needs to be rerolled for PSR-4
Comment #32
seiplax CreditAttribution: seiplax commentedre-rolled after PSR-4
Comment #33
scor CreditAttribution: scor commented32: support-rdfa-output-in-entity-reference-2188895-32.patch queued for re-testing.
Comment #35
lokapujyaJust a reroll.
Comment #36
scor CreditAttribution: scor commentedlooks like we're missing the test which was present in #30.
Comment #38
lokapujyaAdded the test back in.
Comment #40
scor CreditAttribution: scor commentedlooks like #2183231: Make ContentEntityDatabaseStorage generate static database schemas for content entities introduced a change? either way, the test is passing if I use
instead of
Comment #41
lokapujyaIn 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.
Comment #42
scor CreditAttribution: scor commentedLooks great, glad you found the culprit that was causing the URI issue.
we should remove that :)
Comment #43
lokapujyaI was trying to sneak it in, haha.
Comment #44
lokapujyaRemoved the hack.
Comment #45
scor CreditAttribution: scor commentedThis looks good to go, except for this minor comment:
This won't pass coding standards, it's > 80 chars. compacting
should not
toshouldn't
should make it fit on the same line...Comment #46
lokapujyaFixed some comment spacing.
Comment #47
lokapujyaattaching the interdiff for last patch.
Comment #48
scor CreditAttribution: scor commentedThanks Jamie!
Comment #49
alexpottCommitted 227d641 and pushed to 8.x. Thanks!