Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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       // 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;
79     }
80     return $this->target;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getTargetIdentifier() {
87     if (isset($this->id)) {
88       return $this->id;
89     }
90     elseif ($entity = $this->getValue()) {
91       return $entity->id();
92     }
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function setValue($value, $notify = TRUE) {
99     unset($this->target);
100     unset($this->id);
101     unset($this->revision_id);
102
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;
107     }
108     elseif (is_object($value) && $value instanceof EntityInterface) {
109       $this->target = $value->getTypedData();
110     }
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.');
113     }
114     else {
115       $this->id = $value['target_id'];
116       $this->revision_id = $value['target_revision_id'];
117     }
118     // Notify the parent of any changes.
119     if ($notify && isset($this->parent)) {
120       $this->parent->onChange($this->name);
121     }
122   }
123 }