Skip to content
Snippets Groups Projects
Commit 4bdfd068 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2851149 by hchonov, amateescu, Wim Leers: Exceptions on setting entity...

Issue #2851149 by hchonov, amateescu, Wim Leers: Exceptions on setting entity reference field with integer target ID and entity object
parent 6d2c634e
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -189,8 +189,11 @@ public function setValue($values, $notify = TRUE) {
$entity_id = $this->get('entity')->getTargetIdentifier();
// If the entity has been saved and we're trying to set both the
// target_id and the entity values with a non-null target ID, then the
// value for target_id should match the ID of the entity value.
if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id !== $values['target_id'])) {
// value for target_id should match the ID of the entity value. The
// entity ID as returned by $entity->id() might be a string, but the
// provided target_id might be an integer - therefore we have to do a
// non-strict comparison.
if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id != $values['target_id'])) {
throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.');
}
}
......
......@@ -183,6 +183,15 @@ public function testContentEntityReferenceItem() {
$entity->field_test_taxonomy_term->generateSampleItems();
$entity->field_test_taxonomy_vocabulary->generateSampleItems();
$this->entityValidateAndSave($entity);
// Tests that setting an integer target ID together with an entity object
// succeeds and does not cause any exceptions. There is no assertion here,
// as the assignment should not throw any exceptions and if it does the
// test will fail.
// @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue().
$user = User::create(['name' => $this->randomString()]);
$user->save();
$entity = EntityTest::create(['user_id' => ['target_id' => (int) $user->id(), 'entity' => $user]]);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment