Problem/Motivation
EntityReferenceFormatterBase::getEntitiesToView() sets $entity->_referringItem but if the same entity is referred multiple times then the value gets overridden.
Steps to reproduce
Refer the same file twice, add something to $item->_attributes in hook_entity_prepare_view to both of the referring items, observe how GenericFileFormatter makes a total hash of it: the first file link will get the attributes of the second and the second will get nothing.
Proposed resolution
// Add the referring item, in case the formatter needs it.
if ($entity->_referringItem) {
$entity = clone $entity;
}
$entity->_referringItem = $items[$delta];
$entity->_entityReferenceViewLangcode = $langcode;
$entities[$delta] = $entity;
The conditional cloning is new.
Remaining tasks
Make a PR, add a test.
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3489179
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3489179-referring-the-same
changes, plain diff MR !10299
Comments
Comment #2
ghost of drupal pastComment #3
andypostLooks like duplicate of
Comment #4
godotislateI don't think #2940605: Can only intentionally re-render an entity with references 20 times is a duplicate, but the two issues are closely related. I did notice that
_referringItemwould get overwritten while working on #2940605: Can only intentionally re-render an entity with references 20 times, and cloning the entity so that_referringItemis correct might actually help address recursion detection. Though I think in the (edge?) case where you are previewing new entities, cloning might make recursion detection more difficult, but I don't think that's a blocker here.Comment #6
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
Comment #7
ghost of drupal pastThanks for the quick patch!
I suggested checking whether $entity->_referringItem exists first before cloning because I was afraid of the performance hit here.
Comment #8
godotislateMR is up with tests.
Comment #9
godotislateTY to @ghost of drupal past for the review. Replied to comments on the MR. I resolved the open threads, but please feel free to unresolve if they were not addressed satisfactorily.
Comment #10
ghost of drupal pastYes, sorry, I confused which entity is which. This looks good to me, thanks so much for the through tests and the quick resolution of all.
Comment #14
catchThis is a tricky one, but tidy fix that makes sense and nice test coverage.
Committed/pushed to 11.x, cherry-picked to 11.1.x and 10.5.x, thanks!