Problem/Motivation
When the referenced item is deleted targted_id is still present in the reference field, so existing EntityReferenceItem::isEmpty() will return FALSE
This issue causes other functionality to fail. E.g. jsonapi_extras (check child issues).
Proposed resolution
Always check against an entity existense.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | interdiff_3036992-6-3036992-12.txt | 879 bytes | gnanagowthaman sankar |
| #13 | return_false_if_referenced_item_was_deleted-3036992-12.patch | 910 bytes | gnanagowthaman sankar |
Comments
Comment #2
Andrei_tab commentedProposed solution.
Comment #3
Andrei_tab commentedComment #4
Andrei_tab commentedComment #6
Andrei_tab commentedComment #7
Andrei_tab commentedComment #8
rodman1980 commentedComment #11
wim leersComment #12
gnanagowthaman sankar commentedHi Friends,
Here is the patch.
Comment #13
gnanagowthaman sankar commentedComment #14
twodI see where you're going but I think this change would somewhat contradict the interface description:
The structure is not empty if it has
target_iddefined, and I actually find it strange that it checksentityat all.A value currently does exist in the
target_idproperty, if it is a valid reference at that point in time is a separate concern for the caller, no?I find it easier to reconcile this statement
compared to this statement
If this patch is applied
$field->isEmpty()takes a performance hit when entities do exist and it'd be cheaper to doempty($field->target_id) directly when it's expected to not be set, but that's not understood as well by my IDE and has a higher cognitive load when reading compared to .Practically this change affects when I want to be able to tell the difference between a reference existing and if it is not currently valid, but there's also the interface "contract" to consider. It looks to me like it's not concerned about what a contained value actually means, just if it exists.
Comment #15
amateescu commentedEntityReferenceItem::isEmpty()returning FALSE for deleted references is correct because the entity reference field in core does not enforce data integrity. If you need this functionality, there are two options:1) Use a module like https://www.drupal.org/project/entity_reference_integrity or https://www.drupal.org/project/erri to
preventthat would prevent references from being deleted2) Write a custom module (I couldn't find any existing one in contrib) that would cascade the deletion of an entity to all the entity reference fields that use it as a target
Comment #16
wim leers