be6405af4abdf1a3ace551ce2ca0b08e2626390d
[yaffs-website] / web / core / lib / Drupal / Core / Entity / TypedData / EntityDataDefinition.php
1 <?php
2
3 namespace Drupal\Core\Entity\TypedData;
4
5 use Drupal\Core\TypedData\ComplexDataDefinitionBase;
6
7 /**
8  * A typed data definition class for describing entities.
9  */
10 class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDataDefinitionInterface {
11
12   /**
13    * Creates a new entity definition.
14    *
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.
18    *
19    * @return static
20    */
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);
26     }
27     return $definition;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
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."');
37     }
38     $definition = static::create();
39     // Set the passed entity type and bundle.
40     if (isset($parts[1])) {
41       $definition->setEntityTypeId($parts[1]);
42     }
43     if (isset($parts[2])) {
44       $definition->setBundles([$parts[2]]);
45     }
46     return $definition;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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 = [];
59         }
60         else {
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));
66           }
67           else {
68             $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id);
69           }
70         }
71       }
72       else {
73         // No entity type given.
74         $this->propertyDefinitions = [];
75       }
76     }
77     return $this->propertyDefinitions;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getDataType() {
84     $type = 'entity';
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
88       // bundle.
89       if (($bundles = $this->getBundles()) && count($bundles) == 1) {
90         $bundle = reset($bundles);
91         if ($bundle != $entity_type) {
92           $type .= ':' . $bundle;
93         }
94       }
95     }
96     return $type;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getEntityTypeId() {
103     return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function setEntityTypeId($entity_type_id) {
110     return $this->addConstraint('EntityType', $entity_type_id);
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function getBundles() {
117     $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
118     return is_string($bundle) ? [$bundle] : $bundle;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function setBundles(array $bundles = NULL) {
125     if (isset($bundles)) {
126       $this->addConstraint('Bundle', $bundles);
127     }
128     else {
129       // Remove the constraint.
130       unset($this->definition['constraints']['Bundle']);
131     }
132     return $this;
133   }
134
135 }