Commit 15debbd2 authored by webchick's avatar webchick
Browse files

Issue #2386559 by fago, yched, amateescu, jibran, plach:...

Issue #2386559 by fago, yched, amateescu, jibran, plach: ERItem::setValue(array('entity' => $entity) produces broken Items
parent ec4b6972
......@@ -7,7 +7,6 @@
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
......@@ -146,12 +145,27 @@ public function setValue($values, $notify = TRUE) {
$this->set('entity', $values, $notify);
}
else {
// Make sure that the 'entity' property gets set as 'target_id'.
parent::setValue($values, FALSE);
// Support setting the field item with only one property, but make sure
// values stay in sync if only property is passed.
if (isset($values['target_id']) && !isset($values['entity'])) {
$values['entity'] = $values['target_id'];
$this->onChange('target_id', FALSE);
}
elseif (!isset($values['target_id']) && isset($values['entity'])) {
$this->onChange('entity', FALSE);
}
elseif (isset($values['target_id']) && isset($values['entity'])) {
// If both properties are passed, verify the passed values match.
if ($this->get('entity')->getTargetIdentifier() != $values['target_id']) {
throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.');
}
}
// Notify the parent if necessary.
if ($notify && $this->parent) {
$this->parent->onChange($this->getName());
}
parent::setValue($values, $notify);
}
}
/**
......
......@@ -105,10 +105,33 @@ public function testContentEntityReferenceItem() {
));
$term2->save();
$entity->field_test_taxonomy_term->target_id = $term2->id();
// Test all the possible ways of assigning a value.
$entity->field_test_taxonomy_term->target_id = $term->id();
$this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName());
$entity->field_test_taxonomy_term = [['target_id' => $term2->id()]];
$this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term2->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
// Test value assignment via the computed 'entity' property.
$entity->field_test_taxonomy_term->entity = $term;
$this->assertEqual($entity->field_test_taxonomy_term->target_id, $term->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName());
$entity->field_test_taxonomy_term = [['entity' => $term2]];
$this->assertEqual($entity->field_test_taxonomy_term->target_id, $term2->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
// Test assigning an invalid item throws an exception.
try {
$entity->field_test_taxonomy_term = ['target_id' => 'invalid', 'entity' => $term2];
$this->fail('Assigning an invalid item throws an exception.');
}
catch (\InvalidArgumentException $e) {
$this->pass('Assigning an invalid item throws an exception.');
}
// Delete terms so we have nothing to reference and try again
$term->delete();
$term2->delete();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment