6cf57eb1bef5fd6e87d96a7118d0539f95f17e9e
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / DataType / EntityReference.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\DataType;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\TypedData\DataReferenceBase;
7
8 /**
9  * Defines an 'entity_reference' 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')
25  *   ->setTargetDefinition($definition);
26  * @endcode
27  *
28  * @DataType(
29  *   id = "entity_reference",
30  *   label = @Translation("Entity reference"),
31  *   definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
32  * )
33  */
34 class EntityReference extends DataReferenceBase {
35
36   /**
37    * The entity ID.
38    *
39    * @var int|string
40    */
41   protected $id;
42
43   /**
44    * Gets the definition of the referenced entity.
45    *
46    * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
47    *   The reference target's definition.
48    */
49   public function getTargetDefinition() {
50     return $this->definition->getTargetDefinition();
51   }
52
53   /**
54    * Checks whether the target entity has not been saved yet.
55    *
56    * @return bool
57    *   TRUE if the entity is new, FALSE otherwise.
58    */
59   public function isTargetNew() {
60     // If only an ID is given, the reference cannot be a new entity.
61     return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew();
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getTarget() {
68     if (!isset($this->target) && isset($this->id)) {
69       // If we have a valid reference, return the entity's TypedData adapter.
70       $entity = \Drupal::entityTypeManager()
71         ->getStorage($this->getTargetDefinition()->getEntityTypeId())
72         ->load($this->id);
73       $this->target = isset($entity) ? $entity->getTypedData() : NULL;
74     }
75     return $this->target;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getTargetIdentifier() {
82     if (isset($this->id)) {
83       return $this->id;
84     }
85     elseif ($entity = $this->getValue()) {
86       return $entity->id();
87     }
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function setValue($value, $notify = TRUE) {
94     unset($this->target);
95     unset($this->id);
96
97     // Both the entity ID and the entity object may be passed as value. The
98     // reference may also be unset by passing NULL as value.
99     if (!isset($value)) {
100       $this->target = NULL;
101     }
102     elseif ($value instanceof EntityInterface) {
103       $this->target = $value->getTypedData();
104     }
105     elseif (!is_scalar($value) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
106       throw new \InvalidArgumentException('Value is not a valid entity.');
107     }
108     else {
109       $this->id = $value;
110     }
111     // Notify the parent of any changes.
112     if ($notify && isset($this->parent)) {
113       $this->parent->onChange($this->name);
114     }
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function getString() {
121     if ($entity = $this->getValue()) {
122       return $entity->label();
123     }
124     return '';
125   }
126
127 }