3 namespace Drupal\entity_reference_revisions\Plugin\DataType;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\Plugin\DataType\EntityReference;
9 * Defines an 'entity_reference_revisions' data type.
11 * This serves as 'entity' property of entity reference field items and gets
12 * its value set from the parent, i.e. LanguageItem.
14 * The plain value of this reference is the entity object, i.e. an instance of
15 * \Drupal\Core\Entity\EntityInterface. For setting the value the entity object
16 * or the entity ID may be passed.
18 * Note that the definition of the referenced entity's type is required, whereas
19 * defining referencable entity bundle(s) is optional. A reference defining the
20 * type and bundle of the referenced entity can be created as following:
22 * $definition = \Drupal\Core\Entity\EntityDefinition::create($entity_type)
23 * ->addConstraint('Bundle', $bundle);
24 * \Drupal\Core\TypedData\DataReferenceDefinition::create('entity_revision')
25 * ->setTargetDefinition($definition);
29 * id = "entity_revision_reference",
30 * label = @Translation("Entity reference revisions"),
31 * definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
34 class EntityReferenceRevisions extends EntityReference {
37 * The entity revision ID.
41 protected $revision_id;
51 * Returns the definition of the referenced entity.
53 * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
54 * The reference target's definition.
56 public function getTargetDefinition() {
57 return $this->definition->getTargetDefinition();
61 * Checks whether the target entity has not been saved yet.
64 * TRUE if the entity is new, FALSE otherwise.
66 public function isTargetNew() {
67 // If only an ID is given, the reference cannot be a new entity.
68 return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew();
74 public function getTarget() {
75 if (!isset($this->target) && isset($this->revision_id)) {
76 // If we have a valid reference, return the entity's TypedData adapter.
77 $entity = \Drupal::entityTypeManager()->getStorage($this->getTargetDefinition()->getEntityTypeId())->loadRevision($this->revision_id);
78 $this->target = isset($entity) ? $entity->getTypedData() : NULL;
86 public function getTargetIdentifier() {
87 if (isset($this->id)) {
90 elseif ($entity = $this->getValue()) {
98 public function setValue($value, $notify = TRUE) {
101 unset($this->revision_id);
103 // Both the entity ID and the entity object may be passed as value. The
104 // reference may also be unset by passing NULL as value.
105 if (!isset($value)) {
106 $this->target = NULL;
108 elseif (is_object($value) && $value instanceof EntityInterface) {
109 $this->target = $value->getTypedData();
111 elseif (!is_scalar($value['target_id']) || !is_scalar($value['target_revision_id']) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
112 throw new \InvalidArgumentException('Value is not a valid entity.');
115 $this->id = $value['target_id'];
116 $this->revision_id = $value['target_revision_id'];
118 // Notify the parent of any changes.
119 if ($notify && isset($this->parent)) {
120 $this->parent->onChange($this->name);