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.

Comments

Andrei_tab created an issue. See original summary.

Andrei_tab’s picture

Proposed solution.

Andrei_tab’s picture

Status: Active » Needs review
Andrei_tab’s picture

Issue tags: +epam-contrib-2019.03

Status: Needs review » Needs work

The last submitted patch, 2: entity-reference-is-empty-update-3036992-1.patch, failed testing. View results

Andrei_tab’s picture

Andrei_tab’s picture

rodman1980’s picture

Issue tags: -epam-contrib-2019.03

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

wim leers’s picture

Component: plugin system » entity_reference.module
gnanagowthaman sankar’s picture

Hi Friends,

Here is the patch.

gnanagowthaman sankar’s picture

twod’s picture

I see where you're going but I think this change would somewhat contradict the interface description:

* Determines whether the data structure is empty.

The structure is not empty if it has target_id defined, and I actually find it strange that it checks entity at all.

A value currently does exist in the target_id property, 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

$field->isEmpty() === FALSE &&
$field->target_id === 123 &&
$field->getValue() == [['target_id' => 123] &&
$field->entity === NULL

compared to this statement

$field->isEmpty() === TRUE &&
$field->target_id === 123 &&
$field->getValue() == [['target_id' => 123] &&
$field->entity === NULL

If this patch is applied $field->isEmpty() takes a performance hit when entities do exist and it'd be cheaper to do empty($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.

amateescu’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Needs work » Fixed
Issue tags: -Entity Reference, -Fields in Core

EntityReferenceItem::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 prevent that would prevent references from being deleted

2) 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

wim leers’s picture

Issue tags: +data integrity

Status: Fixed » Closed (fixed)

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