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, $bundle = NULL) {
22 // If the entity type is known, use the derived definition.
23 if (isset($entity_type_id)) {
24 $data_type = "entity:{$entity_type_id}";
26 // If a bundle was given, use the bundle-specific definition.
28 $data_type .= ":{$bundle}";
31 // It's possible that the given entity type ID or bundle wasn't discovered
32 // by the TypedData plugin manager and/or weren't created by the
33 // EntityDeriver. In that case, this is a new definition and we'll just
34 // create the definition from defaults by using an empty array.
35 $values = \Drupal::typedDataManager()->getDefinition($data_type, FALSE);
36 $definition = new static(is_array($values) ? $values : []);
38 // Set the EntityType constraint using the given entity type ID.
39 $definition->setEntityTypeId($entity_type_id);
41 // If available, set the Bundle constraint.
43 $definition->setBundles([$bundle]);
49 return new static([]);
55 public static function createFromDataType($data_type) {
56 $parts = explode(':', $data_type);
57 if ($parts[0] != 'entity') {
58 throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
60 return static::create(
61 isset($parts[1]) ? $parts[1] : NULL,
62 isset($parts[2]) ? $parts[2] : NULL
69 public function getPropertyDefinitions() {
70 if (!isset($this->propertyDefinitions)) {
71 if ($entity_type_id = $this->getEntityTypeId()) {
72 // Return an empty array for entities that are not content entities.
73 $entity_type_class = \Drupal::entityManager()->getDefinition($entity_type_id)->getClass();
74 if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
75 $this->propertyDefinitions = [];
78 // @todo: Add support for handling multiple bundles.
79 // See https://www.drupal.org/node/2169813.
80 $bundles = $this->getBundles();
81 if (is_array($bundles) && count($bundles) == 1) {
82 $this->propertyDefinitions = \Drupal::entityManager()->getFieldDefinitions($entity_type_id, reset($bundles));
85 $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id);
90 // No entity type given.
91 $this->propertyDefinitions = [];
94 return $this->propertyDefinitions;
100 public function getDataType() {
102 if ($entity_type = $this->getEntityTypeId()) {
103 $type .= ':' . $entity_type;
104 // Append the bundle only if we know it for sure and it is not the default
106 if (($bundles = $this->getBundles()) && count($bundles) == 1) {
107 $bundle = reset($bundles);
108 if ($bundle != $entity_type) {
109 $type .= ':' . $bundle;
119 public function getEntityTypeId() {
120 return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
126 public function setEntityTypeId($entity_type_id) {
127 return $this->addConstraint('EntityType', $entity_type_id);
133 public function getBundles() {
134 $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
135 return is_string($bundle) ? [$bundle] : $bundle;
141 public function setBundles(array $bundles = NULL) {
142 if (isset($bundles)) {
143 $this->addConstraint('Bundle', $bundles);
146 // Remove the constraint.
147 unset($this->definition['constraints']['Bundle']);