EntityAdapter.php 4.5 KB

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