function EntityReference::setValue

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference::setValue()
  2. 8.9.x core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference::setValue()
  3. 10 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference::setValue()

Overrides DataReferenceBase::setValue

File

core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php, line 95

Class

EntityReference
Defines an 'entity_reference' data type.

Namespace

Drupal\Core\Entity\Plugin\DataType

Code

public function setValue($value, $notify = TRUE) {
  unset($this->target);
  unset($this->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 ($value instanceof EntityInterface) {
    $this->target = $value->getTypedData();
  }
  elseif (!is_scalar($value) || $this->getTargetDefinition()
    ->getEntityTypeId() === NULL) {
    throw new \InvalidArgumentException('Value is not a valid entity.');
  }
  else {
    $this->id = $value;
  }
  // Notify the parent of any changes.
  if ($notify && isset($this->parent)) {
    $this->parent
      ->onChange($this->name);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.