Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
On Entity Reference validation, the current field value's parent entity isn't being submitted to the field handler. This could cause problems for handlers that configure the reference query based on the parent entity. It causes the initial query to not match the follow-up validation query.
Adding a patch to add the current value's parent entity to the handler on initialization.
Comment | File | Size | Author |
---|---|---|---|
#4 | ValidReferenceConstraintValidator-parent-entity.diff | 1.13 KB | grathbone |
ValidReferenceConstraintValidator-parent-entity.diff | 1.03 KB | grathbone | |
Comments
Comment #3
grathbone CreditAttribution: grathbone commentedComment #4
grathbone CreditAttribution: grathbone as a volunteer commentedAdded a check on the parent. Now passes in default NULL as a fallback.
Comment #5
grathbone CreditAttribution: grathbone as a volunteer commentedComment #6
Sam152 CreditAttribution: Sam152 commentedRemoving the tags as per the guidelines: https://www.drupal.org/issue-tags. Generally not adding a tag unless aids discovery in a way that the other fields don't allow for. In this case we have the component, which I think covers it.
This fix makes total sense. Why not pass the entity through to the plugin manager if we have it. Small item of feedback:
The FieldItemListInterface specifies:
So I think it's safe to assume you'll always get an entity from ->getParent(). Thus I think we can simply pass $value->getParent() to $this->selectionManager->getSelectionHandler.
For me, this is right on the line for needing a test. It uses an API in a way which seems obviously more correct, but wondering if there is a test case which obviously exposes this as a bug we can use? We'd possibly need a test handler which uses the entity, but since it's an optional param, most handlers should work with NULL if possible anyway.
Comment #7
grathbone CreditAttribution: grathbone as a volunteer commented@sam152
Check out the unit test results from my first patch, where getParent() does return NULL. And since getEntity() is just a wrapper method that calls $this->getParent()->getValue() anyway, it succumbs to the same fate.
Also, since the EntityFieldTest class does test the issue, I'm not sure we'd need to write another test.
The other option would be to use getEntity() and then do an IF-statement in the \Drupal\Core\Field\FieldItemList::getEntity() method.
Going to switch this back to Needs Review for further input.
Comment #8
grathbone CreditAttribution: grathbone as a volunteer commentedComment #9
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedI just RTBC'd a very similar patch in #2879918: Send entity to selection handler on validate., so I'm going to close this one as a duplicate.
@grathbone, thanks for the patch, I asked the core committers to credit you in that issue :)
Comment #10
grathbone CreditAttribution: grathbone as a volunteer commented@amateescu Coolio. Thank you!