*
* @return static
*/
- public static function create($entity_type_id = NULL) {
- $definition = new static([]);
- // Set the passed entity type.
+ public static function create($entity_type_id = NULL, $bundle = NULL) {
+ // If the entity type is known, use the derived definition.
if (isset($entity_type_id)) {
+ $data_type = "entity:{$entity_type_id}";
+
+ // If a bundle was given, use the bundle-specific definition.
+ if ($bundle) {
+ $data_type .= ":{$bundle}";
+ }
+
+ // It's possible that the given entity type ID or bundle wasn't discovered
+ // by the TypedData plugin manager and/or weren't created by the
+ // EntityDeriver. In that case, this is a new definition and we'll just
+ // create the definition from defaults by using an empty array.
+ $values = \Drupal::typedDataManager()->getDefinition($data_type, FALSE);
+ $definition = new static(is_array($values) ? $values : []);
+
+ // Set the EntityType constraint using the given entity type ID.
$definition->setEntityTypeId($entity_type_id);
+
+ // If available, set the Bundle constraint.
+ if ($bundle) {
+ $definition->setBundles([$bundle]);
+ }
+
+ return $definition;
}
- return $definition;
+
+ return new static([]);
}
/**
if ($parts[0] != 'entity') {
throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
}
- $definition = static::create();
- // Set the passed entity type and bundle.
- if (isset($parts[1])) {
- $definition->setEntityTypeId($parts[1]);
- }
- if (isset($parts[2])) {
- $definition->setBundles([$parts[2]]);
- }
- return $definition;
+ return static::create(
+ isset($parts[1]) ? $parts[1] : NULL,
+ isset($parts[2]) ? $parts[2] : NULL
+ );
}
/**