EntityDataDefinition.php in Drupal 10
File
core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php
View source
<?php
namespace Drupal\Core\Entity\TypedData;
use Drupal\Core\TypedData\ComplexDataDefinitionBase;
class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDataDefinitionInterface {
public static function create($entity_type_id = NULL, $bundle = NULL) {
if (isset($entity_type_id)) {
$data_type = "entity:{$entity_type_id}";
if ($bundle) {
$data_type .= ":{$bundle}";
}
$values = \Drupal::typedDataManager()
->getDefinition($data_type, FALSE);
$definition = new static(is_array($values) ? $values : []);
$definition
->setEntityTypeId($entity_type_id);
if ($bundle) {
$definition
->setBundles([
$bundle,
]);
}
return $definition;
}
return new static([]);
}
public static function createFromDataType($data_type) {
$parts = explode(':', $data_type);
if ($parts[0] != 'entity') {
throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
}
return static::create($parts[1] ?? NULL, $parts[2] ?? NULL);
}
public function getPropertyDefinitions() {
if (!isset($this->propertyDefinitions)) {
if ($entity_type_id = $this
->getEntityTypeId()) {
$entity_type_class = \Drupal::entityTypeManager()
->getDefinition($entity_type_id)
->getClass();
if (!in_array('Drupal\\Core\\Entity\\FieldableEntityInterface', class_implements($entity_type_class))) {
$this->propertyDefinitions = [];
}
else {
$bundles = $this
->getBundles();
if (is_array($bundles) && count($bundles) == 1) {
$this->propertyDefinitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type_id, reset($bundles));
}
else {
$this->propertyDefinitions = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions($entity_type_id);
}
}
}
else {
$this->propertyDefinitions = [];
}
}
return $this->propertyDefinitions;
}
public function getDataType() {
$type = 'entity';
if ($entity_type = $this
->getEntityTypeId()) {
$type .= ':' . $entity_type;
if (($bundles = $this
->getBundles()) && count($bundles) == 1) {
$bundle = reset($bundles);
if ($bundle != $entity_type) {
$type .= ':' . $bundle;
}
}
}
return $type;
}
public function getEntityTypeId() {
return $this->definition['constraints']['EntityType'] ?? NULL;
}
public function setEntityTypeId($entity_type_id) {
return $this
->addConstraint('EntityType', $entity_type_id);
}
public function getBundles() {
$bundle = $this->definition['constraints']['Bundle'] ?? NULL;
return is_string($bundle) ? [
$bundle,
] : $bundle;
}
public function setBundles(array $bundles = NULL) {
if (isset($bundles)) {
$this
->addConstraint('Bundle', $bundles);
}
else {
unset($this->definition['constraints']['Bundle']);
}
return $this;
}
}