3 namespace Drupal\Core\Entity\Plugin\DataType;
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;
13 * Defines the "entity" data type.
15 * Instances of this class wrap entity objects and allow to deal with entities
16 * based upon the Typed Data API.
18 * In addition to the "entity" data type, this exposes derived
19 * "entity:$entity_type" and "entity:$entity_type:$bundle" data types.
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"
29 class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexDataInterface {
32 * The wrapped entity object.
34 * @var \Drupal\Core\Entity\EntityInterface|null
39 * Creates an instance wrapping the given entity.
41 * @param \Drupal\Core\Entity\EntityInterface|null $entity
42 * The entity object to wrap.
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);
58 public function getValue() {
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);
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.");
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.");
85 // This will throw an exception for unknown fields.
86 return $this->entity->get($property_name);
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.");
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.");
101 // This will throw an exception for unknown fields.
102 $this->entity->set($property_name, $value, $notify);
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.');
113 if (!$this->entity instanceof FieldableEntityInterface) {
114 // @todo: Add support for config entities in
115 // https://www.drupal.org/node/1818574.
118 return $this->entity->getFields($include_computed);
124 public function toArray() {
125 if (!isset($this->entity)) {
126 throw new MissingDataException('Unable to get property values as no entity has been provided.');
128 return $this->entity->toArray();
134 public function isEmpty() {
135 return !isset($this->entity);
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);
151 public function getString() {
152 return isset($this->entity) ? $this->entity->label() : '';
158 public function applyDefaultValue($notify = TRUE) {
159 // Apply the default value of all properties.
160 foreach ($this->getProperties() as $property) {
161 $property->applyDefaultValue(FALSE);
169 public function getIterator() {
170 return isset($this->entity) ? $this->entity->getIterator() : new \ArrayIterator([]);