Issue #2936725 by gabesullice, Wim Leers, dawehner, Berdir: EntityDataDefinition::create() does not respect derived entity definitions

merge-requests/1654/head
Lee Rowlands 2018-01-24 07:31:04 +10:00
parent f69af23fa0
commit b2b7f4665a
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
3 changed files with 47 additions and 16 deletions

View File

@ -18,13 +18,35 @@ class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDa
* *
* @return static * @return static
*/ */
public static function create($entity_type_id = NULL) { public static function create($entity_type_id = NULL, $bundle = NULL) {
$definition = new static([]); // If the entity type is known, use the derived definition.
// Set the passed entity type.
if (isset($entity_type_id)) { 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); $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') { if ($parts[0] != 'entity') {
throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."'); throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
} }
$definition = static::create(); return static::create(
// Set the passed entity type and bundle. isset($parts[1]) ? $parts[1] : NULL,
if (isset($parts[1])) { isset($parts[2]) ? $parts[2] : NULL
$definition->setEntityTypeId($parts[1]); );
}
if (isset($parts[2])) {
$definition->setBundles([$parts[2]]);
}
return $definition;
} }
/** /**

View File

@ -93,8 +93,10 @@ class EntityViewsDataTest extends UnitTestCase {
$typed_data_manager->expects($this->any()) $typed_data_manager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->equalTo('field_item:string_long')) ->will($this->returnValueMap([
->willReturn(['class' => '\Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem']); 'entity:user' => ['class' => '\Drupal\Core\TypedData\DataDefinitionInterface'],
'field_item:string_long' => ['class' => '\Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem'],
]));
$this->baseEntityType = new TestEntityType([ $this->baseEntityType = new TestEntityType([
'base_table' => 'entity_test', 'base_table' => 'entity_test',

View File

@ -11,6 +11,7 @@ use Drupal\Core\TypedData\DataReferenceDefinition;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface; use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\Core\TypedData\ListDataDefinitionInterface; use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\KernelTests\KernelTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
/** /**
* Tests deriving metadata of entity and field data types. * Tests deriving metadata of entity and field data types.
@ -31,10 +32,16 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
* *
* @var array * @var array
*/ */
public static $modules = ['filter', 'text', 'node', 'user']; public static $modules = ['system', 'filter', 'text', 'node', 'user'];
protected function setUp() { protected function setUp() {
parent::setup(); parent::setup();
NodeType::create([
'type' => 'article',
'name' => 'Article',
])->save();
$this->typedDataManager = $this->container->get('typed_data_manager'); $this->typedDataManager = $this->container->get('typed_data_manager');
} }
@ -82,10 +89,15 @@ class EntityTypedDataDefinitionTest extends KernelTestBase {
*/ */
public function testEntities() { public function testEntities() {
$entity_definition = EntityDataDefinition::create('node'); $entity_definition = EntityDataDefinition::create('node');
$bundle_definition = EntityDataDefinition::create('node', 'article');
// Entities are complex data. // Entities are complex data.
$this->assertFalse($entity_definition instanceof ListDataDefinitionInterface); $this->assertFalse($entity_definition instanceof ListDataDefinitionInterface);
$this->assertTrue($entity_definition instanceof ComplexDataDefinitionInterface); $this->assertTrue($entity_definition instanceof ComplexDataDefinitionInterface);
// Entity definitions should inherit their labels from the entity type.
$this->assertEquals('Content', $entity_definition->getLabel());
$this->assertEquals('Article', $bundle_definition->getLabel());
$field_definitions = $entity_definition->getPropertyDefinitions(); $field_definitions = $entity_definition->getPropertyDefinitions();
// Comparison should ignore the internal static cache, so compare the // Comparison should ignore the internal static cache, so compare the
// serialized objects instead. // serialized objects instead.