Problem/Motivation

When setting an entity reference field with target ID as integer and next to it provide the entity object an exception will be thrown.

Proposed resolution

The reason is that in EntityReferenceItem::setValue we do a type comparison:

$entity_id = $this->get('entity')->getTargetIdentifier();
...
$entity_id !== $values['target_id']

However $entity->id() returns the ID as a string, so the comparison will fail.

We should compare with "!=" instead of with "!==".

Remaining tasks

Review & Commit.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hchonov created an issue. See original summary.

hchonov’s picture

hchonov’s picture

I've been asked in IRC by @amateescu to put the code snippet to an existing test method, this is the only change to the PASS patch.

amateescu’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Needs review » Reviewed & tested by the community

Looks great :)

The last submitted patch, 2: 2851149-2-FAIL.patch, failed testing.

Wim Leers’s picture

However $entity->id() returns the ID as a string, so the comparison will fail.

/facepalm

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
@@ -190,7 +190,7 @@ public function setValue($values, $notify = TRUE) {
-        if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id !== $values['target_id'])) {
+        if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id != $values['target_id'])) {

I think it'd be good to add an explicit comment here about the use of a non-strict comparison.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Yes let's add the comment. Or alternatively should we cast the result of $entity->id() somewhere beforehand?

amateescu’s picture

Or alternatively should we cast the result of $entity->id() somewhere beforehand?

That's a bit dangerous because entity IDs can be integers or strings, and I don't think looking at the base field definitions just for this cast is justifiable.

hchonov’s picture

Adding a comment why we have to do a non-strict comparison.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Nice :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 2851149-9.patch, failed testing.

hchonov’s picture

Status: Needs work » Reviewed & tested by the community

It has been a CI error, therefore putting back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs re-roll

This looks good to me, the @todos point to an issue that's close to ready but isn't going to make it into 8.3.x, whereas this still potentially could. Doesn't apply to 8.4.x though.

hchonov’s picture

@catch, I've just tried to apply the patch and it applies on both 8.3.x and 8.4.x. I've put it for retesting once more for both 8.3.x and 8.4.x just in case.

hchonov’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs re-roll

It looks like that the patch is applying as the tests are currently running, therefore I am removing the "Needs re-roll" tag and putting it back to RTBC as nothing has been changed on the patch.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed 4bdfd06 and pushed to 8.4.x. Thanks!

  • alexpott committed 4bdfd06 on 8.4.x
    Issue #2851149 by hchonov, amateescu, Wim Leers: Exceptions on setting...
alexpott’s picture

Status: Patch (to be ported) » Fixed

@catch, @cilefen, @xjm and I just decided that patch rules apply to 8.3.x up until RC2 so we can back port this.

Committed 041b208 and pushed to 8.3.x. Thanks!

  • alexpott committed 041b208 on 8.3.x
    Issue #2851149 by hchonov, amateescu, Wim Leers: Exceptions on setting...
joelpittet’s picture

Status: Fixed » Reviewed & tested by the community
FileSize
2.89 KB
joelpittet’s picture

Status: Reviewed & tested by the community » Fixed

Whoops wrong issue.

Status: Fixed » Closed (fixed)

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