3 namespace Drupal\Core\Entity\TypedData;
5 use Drupal\Core\TypedData\ComplexDataDefinitionBase;
8 * A typed data definition class for describing entities.
10 class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDataDefinitionInterface {
13 * Creates a new entity definition.
15 * @param string $entity_type_id
16 * (optional) The ID of the entity type, or NULL if the entity type is
17 * unknown. Defaults to NULL.
21 public static function create($entity_type_id = NULL) {
22 $definition = new static([]);
23 // Set the passed entity type.
24 if (isset($entity_type_id)) {
25 $definition->setEntityTypeId($entity_type_id);
33 public static function createFromDataType($data_type) {
34 $parts = explode(':', $data_type);
35 if ($parts[0] != 'entity') {
36 throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
38 $definition = static::create();
39 // Set the passed entity type and bundle.
40 if (isset($parts[1])) {
41 $definition->setEntityTypeId($parts[1]);
43 if (isset($parts[2])) {
44 $definition->setBundles([$parts[2]]);
52 public function getPropertyDefinitions() {
53 if (!isset($this->propertyDefinitions)) {
54 if ($entity_type_id = $this->getEntityTypeId()) {
55 // Return an empty array for entities that are not content entities.
56 $entity_type_class = \Drupal::entityManager()->getDefinition($entity_type_id)->getClass();
57 if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
58 $this->propertyDefinitions = [];
61 // @todo: Add support for handling multiple bundles.
62 // See https://www.drupal.org/node/2169813.
63 $bundles = $this->getBundles();
64 if (is_array($bundles) && count($bundles) == 1) {
65 $this->propertyDefinitions = \Drupal::entityManager()->getFieldDefinitions($entity_type_id, reset($bundles));
68 $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id);
73 // No entity type given.
74 $this->propertyDefinitions = [];
77 return $this->propertyDefinitions;
83 public function getDataType() {
85 if ($entity_type = $this->getEntityTypeId()) {
86 $type .= ':' . $entity_type;
87 // Append the bundle only if we know it for sure and it is not the default
89 if (($bundles = $this->getBundles()) && count($bundles) == 1) {
90 $bundle = reset($bundles);
91 if ($bundle != $entity_type) {
92 $type .= ':' . $bundle;
102 public function getEntityTypeId() {
103 return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
109 public function setEntityTypeId($entity_type_id) {
110 return $this->addConstraint('EntityType', $entity_type_id);
116 public function getBundles() {
117 $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
118 return is_string($bundle) ? [$bundle] : $bundle;
124 public function setBundles(array $bundles = NULL) {
125 if (isset($bundles)) {
126 $this->addConstraint('Bundle', $bundles);
129 // Remove the constraint.
130 unset($this->definition['constraints']['Bundle']);