EntityAdapter.php in Drupal 9
File
core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php
View source
<?php
namespace Drupal\Core\Entity\Plugin\DataType;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedData;
class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexDataInterface {
protected $entity;
public static function createFromEntity(EntityInterface $entity) {
$definition = EntityDataDefinition::create()
->setEntityTypeId($entity
->getEntityTypeId())
->setBundles([
$entity
->bundle(),
]);
$instance = new static($definition);
$instance
->setValue($entity);
return $instance;
}
public function getValue() {
return $this->entity;
}
public function setValue($entity, $notify = TRUE) {
$this->entity = $entity;
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function get($property_name) {
if (!isset($this->entity)) {
throw new MissingDataException("Unable to get property {$property_name} as no entity has been provided.");
}
if (!$this->entity instanceof FieldableEntityInterface) {
throw new \InvalidArgumentException("Unable to get unknown property {$property_name}.");
}
return $this->entity
->get($property_name);
}
public function set($property_name, $value, $notify = TRUE) {
if (!isset($this->entity)) {
throw new MissingDataException("Unable to set property {$property_name} as no entity has been provided.");
}
if (!$this->entity instanceof FieldableEntityInterface) {
throw new \InvalidArgumentException("Unable to set unknown property {$property_name}.");
}
$this->entity
->set($property_name, $value, $notify);
return $this;
}
public function getProperties($include_computed = FALSE) {
if (!isset($this->entity)) {
throw new MissingDataException('Unable to get properties as no entity has been provided.');
}
if (!$this->entity instanceof FieldableEntityInterface) {
return [];
}
return $this->entity
->getFields($include_computed);
}
public function toArray() {
if (!isset($this->entity)) {
throw new MissingDataException('Unable to get property values as no entity has been provided.');
}
return $this->entity
->toArray();
}
public function isEmpty() {
return !isset($this->entity);
}
public function onChange($property_name) {
if (isset($this->entity) && $this->entity instanceof FieldableEntityInterface) {
$this->entity
->onChange($property_name);
}
}
public function getString() {
return isset($this->entity) ? $this->entity
->label() : '';
}
public function applyDefaultValue($notify = TRUE) {
foreach ($this
->getProperties() as $property) {
$property
->applyDefaultValue(FALSE);
}
return $this;
}
public function getIterator() {
return $this->entity instanceof \IteratorAggregate ? $this->entity
->getIterator() : new \ArrayIterator([]);
}
public function getEntity() {
return $this->entity;
}
}