Pull merge.
[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, $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}";
25
26       // If a bundle was given, use the bundle-specific definition.
27       if ($bundle) {
28         $data_type .= ":{$bundle}";
29       }
30
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 : []);
37
38       // Set the EntityType constraint using the given entity type ID.
39       $definition->setEntityTypeId($entity_type_id);
40
41       // If available, set the Bundle constraint.
42       if ($bundle) {
43         $definition->setBundles([$bundle]);
44       }
45
46       return $definition;
47     }
48
49     return new static([]);
50   }
51
52   /**
53    * {@inheritdoc}
54    */
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."');
59     }
60     return static::create(
61       isset($parts[1]) ? $parts[1] : NULL,
62       isset($parts[2]) ? $parts[2] : NULL
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
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 = [];
76         }
77         else {
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));
83           }
84           else {
85             $this->propertyDefinitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id);
86           }
87         }
88       }
89       else {
90         // No entity type given.
91         $this->propertyDefinitions = [];
92       }
93     }
94     return $this->propertyDefinitions;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function getDataType() {
101     $type = 'entity';
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
105       // bundle.
106       if (($bundles = $this->getBundles()) && count($bundles) == 1) {
107         $bundle = reset($bundles);
108         if ($bundle != $entity_type) {
109           $type .= ':' . $bundle;
110         }
111       }
112     }
113     return $type;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function getEntityTypeId() {
120     return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function setEntityTypeId($entity_type_id) {
127     return $this->addConstraint('EntityType', $entity_type_id);
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function getBundles() {
134     $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
135     return is_string($bundle) ? [$bundle] : $bundle;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function setBundles(array $bundles = NULL) {
142     if (isset($bundles)) {
143       $this->addConstraint('Bundle', $bundles);
144     }
145     else {
146       // Remove the constraint.
147       unset($this->definition['constraints']['Bundle']);
148     }
149     return $this;
150   }
151
152 }