Loading src/Plugin/DataType/DynamicEntityReference.php +9 −2 Original line number Diff line number Diff line Loading @@ -38,9 +38,15 @@ class DynamicEntityReference extends EntityReference { if (!isset($this->target) && isset($this->id)) { // If we have a valid reference, return the entity object which is typed // data itself. $entity = entity_load($this->parent->getValue()['target_type'], $this->id); $target_type = $this->getTargetDefinition()->getEntityTypeId() ?: $this->parent->getValue()['target_type']; $entity = entity_load($target_type, $this->id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } // Keep the entity-type in sync. if ($this->target) { $this->getTargetDefinition()->setEntityTypeId($this->target->getValue() ->getEntityTypeId()); } return $this->target; } Loading @@ -58,8 +64,9 @@ class DynamicEntityReference extends EntityReference { } elseif ($value instanceof EntityInterface) { $this->target = $value->getTypedData(); $this->getTargetDefinition()->setEntityTypeId($value->getEntityTypeId()); } elseif (!is_scalar($value) || $this->parent->getValue()['target_type'] === NULL) { elseif (!is_scalar($value) || $this->getTargetDefinition()->getEntityTypeId() === NULL) { throw new \InvalidArgumentException('Value is not a valid entity.'); } else { Loading src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +6 −8 Original line number Diff line number Diff line Loading @@ -124,17 +124,15 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem { /** * {@inheritdoc} */ public function onChange($property_name) { // Make sure that the target ID and type and the target property stay in // sync. if ($property_name == 'target_id') { $this->properties['entity']->setValue($this->target_id, FALSE); public function onChange($property_name, $notify = TRUE) { if ($property_name == 'target_type') { $this->get('entity')->getDataDefinition()->getTargetDefinition()->setEntityTypeId($this->get('target_type')->getValue()); } // Make sure that the target type and the target property stay in sync. elseif ($property_name == 'entity') { $this->set('target_id', $this->properties['entity']->getTargetIdentifier(), FALSE); $this->set('target_type', $this->properties['entity']->getValue()->getEntityTypeId(), FALSE); $this->writePropertyValue('target_type', $this->get('entity')->getValue()->getEntityTypeId()); } parent::onChange($property_name); parent::onChange($property_name, $notify); } /** Loading Loading
src/Plugin/DataType/DynamicEntityReference.php +9 −2 Original line number Diff line number Diff line Loading @@ -38,9 +38,15 @@ class DynamicEntityReference extends EntityReference { if (!isset($this->target) && isset($this->id)) { // If we have a valid reference, return the entity object which is typed // data itself. $entity = entity_load($this->parent->getValue()['target_type'], $this->id); $target_type = $this->getTargetDefinition()->getEntityTypeId() ?: $this->parent->getValue()['target_type']; $entity = entity_load($target_type, $this->id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } // Keep the entity-type in sync. if ($this->target) { $this->getTargetDefinition()->setEntityTypeId($this->target->getValue() ->getEntityTypeId()); } return $this->target; } Loading @@ -58,8 +64,9 @@ class DynamicEntityReference extends EntityReference { } elseif ($value instanceof EntityInterface) { $this->target = $value->getTypedData(); $this->getTargetDefinition()->setEntityTypeId($value->getEntityTypeId()); } elseif (!is_scalar($value) || $this->parent->getValue()['target_type'] === NULL) { elseif (!is_scalar($value) || $this->getTargetDefinition()->getEntityTypeId() === NULL) { throw new \InvalidArgumentException('Value is not a valid entity.'); } else { Loading
src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +6 −8 Original line number Diff line number Diff line Loading @@ -124,17 +124,15 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem { /** * {@inheritdoc} */ public function onChange($property_name) { // Make sure that the target ID and type and the target property stay in // sync. if ($property_name == 'target_id') { $this->properties['entity']->setValue($this->target_id, FALSE); public function onChange($property_name, $notify = TRUE) { if ($property_name == 'target_type') { $this->get('entity')->getDataDefinition()->getTargetDefinition()->setEntityTypeId($this->get('target_type')->getValue()); } // Make sure that the target type and the target property stay in sync. elseif ($property_name == 'entity') { $this->set('target_id', $this->properties['entity']->getTargetIdentifier(), FALSE); $this->set('target_type', $this->properties['entity']->getValue()->getEntityTypeId(), FALSE); $this->writePropertyValue('target_type', $this->get('entity')->getValue()->getEntityTypeId()); } parent::onChange($property_name); parent::onChange($property_name, $notify); } /** Loading