You are here

public function EntityReferenceRevisions::setValue in Entity Reference Revisions 8

Sets the data value.

Parameters

mixed|null $value: The value to set in the format as documented for the data type or NULL to unset the data value.

bool $notify: (optional) Whether to notify the parent object of the change. Defaults to TRUE. If a property is updated from a parent object, set it to FALSE to avoid being notified again.

Throws

\InvalidArgumentException If the value input is inappropriate.

\Drupal\Core\TypedData\Exception\ReadOnlyException If the data is read-only.

Overrides EntityReference::setValue

File

src/Plugin/DataType/EntityReferenceRevisions.php, line 103

Class

EntityReferenceRevisions
Defines an 'entity_reference_revisions' data type.

Namespace

Drupal\entity_reference_revisions\Plugin\DataType

Code

public function setValue($value, $notify = TRUE) {
  unset($this->target);
  unset($this->id);
  unset($this->revision_id);

  // Both the entity ID and the entity object may be passed as value. The
  // reference may also be unset by passing NULL as value.
  if (!isset($value)) {
    $this->target = NULL;
  }
  elseif (is_object($value) && $value instanceof EntityInterface) {
    $this->target = $value
      ->getTypedData();
  }
  elseif (!is_scalar($value['target_id']) || !is_scalar($value['target_revision_id']) || $this
    ->getTargetDefinition()
    ->getEntityTypeId() === NULL) {
    throw new \InvalidArgumentException('Value is not a valid entity.');
  }
  else {
    $this->id = $value['target_id'];
    $this->revision_id = $value['target_revision_id'];
  }

  // Notify the parent of any changes.
  if ($notify && isset($this->parent)) {
    $this->parent
      ->onChange($this->name);
  }
}