diff --git a/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php b/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php index be6405af4a..ea081ddb60 100644 --- a/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php +++ b/core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php @@ -18,13 +18,25 @@ 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)) { + // If a bundle was given, use the bundle-specific definition. + $data_type = isset($bundle) ? + sprintf('entity:%s:%s', $entity_type_id, $bundle) : + sprintf('entity:%s', $entity_type_id); + + // If the definition doesn't exist, just create from defaults. + $values = \Drupal::typedDataManager()->getDefinition($data_type, FALSE); + $definition = new static(is_array($values) ? $values : []); + + // Set the passed entity type. $definition->setEntityTypeId($entity_type_id); + + return $definition; } - return $definition; + + return new static([]); } /** @@ -35,13 +47,15 @@ public static function createFromDataType($data_type) { if ($parts[0] != 'entity') { throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."'); } - $definition = static::create(); + $entity_type_id = isset($parts[1]) ? $parts[1] : NULL; + $bundle = isset($parts[2]) ? $parts[2] : NULL; + $definition = static::create($entity_type_id, $bundle); // Set the passed entity type and bundle. - if (isset($parts[1])) { - $definition->setEntityTypeId($parts[1]); + if ($entity_type_id) { + $definition->setEntityTypeId($entity_type_id); } - if (isset($parts[2])) { - $definition->setBundles([$parts[2]]); + if ($bundle) { + $definition->setBundles([$bundle]); } return $definition; } diff --git a/core/modules/views/tests/src/Unit/EntityViewsDataTest.php b/core/modules/views/tests/src/Unit/EntityViewsDataTest.php index d8b63ce418..3cf5bc4b88 100644 --- a/core/modules/views/tests/src/Unit/EntityViewsDataTest.php +++ b/core/modules/views/tests/src/Unit/EntityViewsDataTest.php @@ -93,8 +93,10 @@ protected function setUp() { $typed_data_manager->expects($this->any()) ->method('getDefinition') - ->with($this->equalTo('field_item:string_long')) - ->willReturn(['class' => '\Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem']); + ->will($this->returnValueMap([ + 'entity:user' => $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface'), + 'field_item:string_long' => ['class' => '\Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem'], + ])); $this->baseEntityType = new TestEntityType([ 'base_table' => 'entity_test', diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php index 5e755d9584..9e16f926f5 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php @@ -11,6 +11,7 @@ use Drupal\Core\TypedData\DataReferenceDefinitionInterface; use Drupal\Core\TypedData\ListDataDefinitionInterface; use Drupal\KernelTests\KernelTestBase; +use Drupal\node\Entity\NodeType; /** * Tests deriving metadata of entity and field data types. @@ -31,10 +32,16 @@ class EntityTypedDataDefinitionTest extends KernelTestBase { * * @var array */ - public static $modules = ['filter', 'text', 'node', 'user']; + public static $modules = ['system', 'filter', 'text', 'node', 'user']; protected function setUp() { parent::setup(); + + NodeType::create([ + 'type' => 'article', + 'name' => 'Article', + ])->save(); + $this->typedDataManager = $this->container->get('typed_data_manager'); } @@ -82,10 +89,15 @@ public function testFields() { */ public function testEntities() { $entity_definition = EntityDataDefinition::create('node'); + $bundle_definition = EntityDataDefinition::create('node', 'article'); // Entities are complex data. $this->assertFalse($entity_definition instanceof ListDataDefinitionInterface); $this->assertTrue($entity_definition instanceof ComplexDataDefinitionInterface); + // Entity definitions should inherit their labels from the entity type. + $this->assertEqual($entity_definition->getLabel(), \Drupal::entityTypeManager()->getDefinition('node')->getLabel()); + $this->assertEqual($bundle_definition->getLabel(), \Drupal::service('entity_type.bundle.info')->getBundleInfo('node')['article']['label']); + $field_definitions = $entity_definition->getPropertyDefinitions(); // Comparison should ignore the internal static cache, so compare the // serialized objects instead.