Version 1
[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       // @todo: Add support for config entities in
82       // https://www.drupal.org/node/1818574.
83       throw new \InvalidArgumentException("Unable to get unknown property $property_name.");
84     }
85     // This will throw an exception for unknown fields.
86     return $this->entity->get($property_name);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function set($property_name, $value, $notify = TRUE) {
93     if (!isset($this->entity)) {
94       throw new MissingDataException("Unable to set property $property_name as no entity has been provided.");
95     }
96     if (!$this->entity instanceof FieldableEntityInterface) {
97       // @todo: Add support for config entities in
98       // https://www.drupal.org/node/1818574.
99       throw new \InvalidArgumentException("Unable to set unknown property $property_name.");
100     }
101     // This will throw an exception for unknown fields.
102     $this->entity->set($property_name, $value, $notify);
103     return $this;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getProperties($include_computed = FALSE) {
110     if (!isset($this->entity)) {
111       throw new MissingDataException('Unable to get properties as no entity has been provided.');
112     }
113     if (!$this->entity instanceof FieldableEntityInterface) {
114       // @todo: Add support for config entities in
115       // https://www.drupal.org/node/1818574.
116       return [];
117     }
118     return $this->entity->getFields($include_computed);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function toArray() {
125     if (!isset($this->entity)) {
126       throw new MissingDataException('Unable to get property values as no entity has been provided.');
127     }
128     return $this->entity->toArray();
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function isEmpty() {
135     return !isset($this->entity);
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function onChange($property_name) {
142     if (isset($this->entity) && $this->entity instanceof FieldableEntityInterface) {
143       // Let the entity know of any changes.
144       $this->entity->onChange($property_name);
145     }
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function getString() {
152     return isset($this->entity) ? $this->entity->label() : '';
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function applyDefaultValue($notify = TRUE) {
159     // Apply the default value of all properties.
160     foreach ($this->getProperties() as $property) {
161       $property->applyDefaultValue(FALSE);
162     }
163     return $this;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getIterator() {
170     return isset($this->entity) ? $this->entity->getIterator() : new \ArrayIterator([]);
171   }
172
173 }