Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Entity / TypedData / EntityDataDefinition.php
index be6405af4abdf1a3ace551ce2ca0b08e2626390d..21c05d1e35d3f4097577eb48c182aba6b38a9ff0 100644 (file)
@@ -18,13 +18,35 @@ class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDa
    *
    * @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([]);
   }
 
   /**
@@ -35,15 +57,10 @@ class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDa
     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
+    );
   }
 
   /**