addConstraint('Bundle', $bundle); * \Drupal\Core\TypedData\DataReferenceDefinition::create('entity') * ->setTargetDefinition($definition); * @endcode * * @DataType( * id = "entity_reference", * label = @Translation("Entity reference"), * definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition" * ) */ class EntityReference extends DataReferenceBase { /** * The entity ID. * * @var int|string */ protected $id; /** * Gets the definition of the referenced entity. * * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface * The reference target's definition. */ public function getTargetDefinition() { return $this->definition->getTargetDefinition(); } /** * Checks whether the target entity has not been saved yet. * * @return bool * TRUE if the entity is new, FALSE otherwise. */ public function isTargetNew() { // If only an ID is given, the reference cannot be a new entity. return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew(); } /** * {@inheritdoc} */ public function getTarget() { if (!isset($this->target) && isset($this->id)) { // If we have a valid reference, return the entity's TypedData adapter. $entity = \Drupal::entityTypeManager() ->getStorage($this->getTargetDefinition()->getEntityTypeId()) ->load($this->id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } return $this->target; } /** * {@inheritdoc} */ public function getTargetIdentifier() { if (isset($this->id)) { return $this->id; } elseif ($entity = $this->getValue()) { return $entity->id(); } } /** * {@inheritdoc} */ 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); } } /** * {@inheritdoc} */ public function getString() { if ($entity = $this->getValue()) { return $entity->label(); } return ''; } }