Problem/Motivation

Currently ChainEntityResolver returns NULL if TargetIdResolver returns an empty value. Although NULL is not, empty values like "", "0" and 0 are valid entity ID's. The resolver should not return NULL for those.

An example where this matters is on entity_reference fields where target_id is 0.

Proposed resolution

Replace the if($id = resolve()) {} check with $id = resolve; if (isset($id)) {} in ChainEntityResolver and in EntityReferenceItemNormalizer.

Remaining tasks

None

User interface changes

None

API changes

None

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because 0 is valid ID for anon user
Issue priority Normal
Disruption None
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Issue tags: +Needs tests

Agreed with the issue itself. Let's just ensure that we have test coverage for that in the future.

tim.plunkett’s picture

For ConfigEntities, '' and NULL are not valid IDs. 0 and '0' are valid.
I can't speak for ContentEntities

Arla’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1.55 KB

Yeah, some tests would be needed to prove the issue. Until then, here's the suggested fix in code. I guess it will pass, because afaik no tests care about this yet.

Arla’s picture

This should motivate the fix in ChainEntityResolver. It's my first go with PHPUnit :)

Btw, @Berdir suggested that TargetIdResolver should actually check if the referenced entity exists. Not sure if this issue is the proper place to do that, maybe if we broaden the scope/title a bit.

Also adding a related issue where we want to be able to use, and serialize, 0 for target_id.

The last submitted patch, 4: empty-entity-ids-2327935-4-TESTS-ONLY.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: empty-entity-ids-2327935-4.patch, failed testing.

Anushka-mp’s picture

Anushka-mp’s picture

Status: Needs work » Needs review
larowlan’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Looks good to me, completed the beta eval template too

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice find. I wondering if we should expand coverage of testing entity ids of 0 or '0' since this might not be the only place we have problems.

This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed adb97da and pushed to 8.0.x. Thanks!

  • alexpott committed adb97da on 8.0.x
    Issue #2327935 by Arla, Anushka-mp: Allow empty entity IDs in...
Berdir’s picture

Status: Fixed » Closed (fixed)

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