Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / DataType / EntityAdapter.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\DataType;
4
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\TypedData\EntityDataDefinition;
8 use Drupal\Core\TypedData\ComplexDataInterface;
9 use Drupal\Core\TypedData\Exception\MissingDataException;
10 use Drupal\Core\TypedData\TypedData;
11
12 /**
13  * Defines the "entity" data type.
14  *
15  * Instances of this class wrap entity objects and allow to deal with entities
16  * based upon the Typed Data API.
17  *
18  * In addition to the "entity" data type, this exposes derived
19  * "entity:$entity_type" and "entity:$entity_type:$bundle" data types.
20  *
21  * @DataType(
22  *   id = "entity",
23  *   label = @Translation("Entity"),
24  *   description = @Translation("All kind of entities, e.g. nodes, comments or users."),
25  *   deriver = "\Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver",
26  *   definition_class = "\Drupal\Core\Entity\TypedData\EntityDataDefinition"
27  * )
28  */
29 class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexDataInterface {
30
31   /**
32    * The wrapped entity object.
33    *
34    * @var \Drupal\Core\Entity\EntityInterface|null
35    */
36   protected $entity;
37
38   /**
39    * Creates an instance wrapping the given entity.
40    *
41    * @param \Drupal\Core\Entity\EntityInterface|null $entity
42    *   The entity object to wrap.
43    *
44    * @return static
45    */
46   public static function createFromEntity(EntityInterface $entity) {
47     $definition = EntityDataDefinition::create()
48       ->setEntityTypeId($entity->getEntityTypeId())
49       ->setBundles([$entity->bundle()]);
50     $instance = new static($definition);
51     $instance->setValue($entity);
52     return $instance;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getValue() {
59     return $this->entity;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function setValue($entity, $notify = TRUE) {
66     $this->entity = $entity;
67     // Notify the parent of any changes.
68     if ($notify && isset($this->parent)) {
69       $this->parent->onChange($this->name);
70     }
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function get($property_name) {
77     if (!isset($this->entity)) {
78       throw new MissingDataException("Unable to get property $property_name as no entity has been provided.");
79     }
80     if (!$this->entity instanceof FieldableEntityInterface) {
81       throw new \InvalidArgumentException("Unable to get unknown property $property_name.");
82     }
83     // This will throw an exception for unknown fields.
84     return $this->entity->get($property_name);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function set($property_name, $value, $notify = TRUE) {
91     if (!isset($this->entity)) {
92       throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
93     }
94     if (!$this->entity instanceof FieldableEntityInterface) {
95       throw new \InvalidArgumentException("Unable to set unknown property $property_name.");
96     }
97     // This will throw an exception for unknown fields.
98     $this->entity->set($property_name, $value, $notify);
99     return $this;
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function getProperties($include_computed = FALSE) {
106     if (!isset($this->entity)) {
107       throw new MissingDataException('Unable to get properties as no entity has been provided.');
108     }
109     if (!$this->entity instanceof FieldableEntityInterface) {
110       return [];
111     }
112     return $this->entity->getFields($include_computed);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function toArray() {
119     if (!isset($this->entity)) {
120       throw new MissingDataException('Unable to get property values as no entity has been provided.');
121     }
122     return $this->entity->toArray();
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function isEmpty() {
129     return !isset($this->entity);
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function onChange($property_name) {
136     if (isset($this->entity) && $this->entity instanceof FieldableEntityInterface) {
137       // Let the entity know of any changes.
138       $this->entity->onChange($property_name);
139     }
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function getString() {
146     return isset($this->entity) ? $this->entity->label() : '';
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function applyDefaultValue($notify = TRUE) {
153     // Apply the default value of all properties.
154     foreach ($this->getProperties() as $property) {
155       $property->applyDefaultValue(FALSE);
156     }
157     return $this;
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function getIterator() {
164     return $this->entity instanceof \IteratorAggregate ? $this->entity->getIterator() : new \ArrayIterator([]);
165   }
166
167 }