Commit 7483c116 authored by Jibran Ijaz's avatar Jibran Ijaz
Browse files

Issue #2137309 by jibran: Typed data does not handle set() and onChange() consistently

parent 6dca772c
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -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;
  }

@@ -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 {
+6 −8
Original line number Diff line number Diff line
@@ -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);
  }

  /**