Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / Plugin / DataType / EntityReferenceRevisions.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions\Plugin\DataType;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\Plugin\DataType\EntityReference;
7
8 /**
9  * Defines an 'entity_reference_revisions' data type.
10  *
11  * This serves as 'entity' property of entity reference field items and gets
12  * its value set from the parent, i.e. LanguageItem.
13  *
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.
17  *
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:
21  * @code
22  * $definition = \Drupal\Core\Entity\EntityDefinition::create($entity_type)
23  *   ->addConstraint('Bundle', $bundle);
24  * \Drupal\Core\TypedData\DataReferenceDefinition::create('entity_revision')
25  *   ->setTargetDefinition($definition);
26  * @endcode
27  *
28  * @DataType(
29  *   id = "entity_revision_reference",
30  *   label = @Translation("Entity reference revisions"),
31  *   definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
32  * )
33  */
34 class EntityReferenceRevisions extends EntityReference {
35
36   /**
37    * The entity revision ID.
38    *
39    * @var integer|string
40    */
41   protected $revision_id;
42
43   /**
44    * The entity ID.
45    *
46    * @var integer|string
47    */
48   protected $id;
49
50   /**
51    * Returns the definition of the referenced entity.
52    *
53    * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
54    *   The reference target's definition.
55    */
56   public function getTargetDefinition() {
57     return $this->definition->getTargetDefinition();
58   }
59
60   /**
61    * Checks whether the target entity has not been saved yet.
62    *
63    * @return bool
64    *   TRUE if the entity is new, FALSE otherwise.
65    */
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();
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getTarget() {
75     if (!isset($this->target) && isset($this->revision_id)) {
76       $storage = \Drupal::entityTypeManager()->getStorage($this->getTargetDefinition()->getEntityTypeId());
77       // By default always load the default revision, so caches get used.
78       $entity = $storage->load($this->id);
79       if ($entity !== NULL && $entity->getRevisionId() != $this->revision_id) {
80         // A non-default revision is a referenced, so load this one.
81         $entity = $storage->loadRevision($this->revision_id);
82       }
83       $this->target = isset($entity) ? $entity->getTypedData() : NULL;
84     }
85     return $this->target;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getTargetIdentifier() {
92     if (isset($this->id)) {
93       return $this->id;
94     }
95     elseif ($entity = $this->getValue()) {
96       return $entity->id();
97     }
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function setValue($value, $notify = TRUE) {
104     unset($this->target);
105     unset($this->id);
106     unset($this->revision_id);
107
108     // Both the entity ID and the entity object may be passed as value. The
109     // reference may also be unset by passing NULL as value.
110     if (!isset($value)) {
111       $this->target = NULL;
112     }
113     elseif (is_object($value) && $value instanceof EntityInterface) {
114       $this->target = $value->getTypedData();
115     }
116     elseif (!is_scalar($value['target_id']) || !is_scalar($value['target_revision_id']) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
117       throw new \InvalidArgumentException('Value is not a valid entity.');
118     }
119     else {
120       $this->id = $value['target_id'];
121       $this->revision_id = $value['target_revision_id'];
122     }
123     // Notify the parent of any changes.
124     if ($notify && isset($this->parent)) {
125       $this->parent->onChange($this->name);
126     }
127   }
128 }