diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index 64597e7..79c092e 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -306,14 +306,6 @@ public function hasKey($key) { /** * {@inheritdoc} */ - public function setKey($key, $value) { - $this->entity_keys[$key] = $value; - return $this; - } - - /** - * {@inheritdoc} - */ public function id() { return $this->id; } diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index 1c3eca5..7e8662b 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -141,18 +141,6 @@ public function getKey($key); public function hasKey($key); /** - * Sets a specific entity key. - * - * @param string $key - * The name of the entity key. - * @param string $value - * The new value of the key. - * - * @return $this - */ - public function setKey($key, $value); - - /** * Indicates whether entities should be statically cached. * * @return bool diff --git a/core/modules/views/src/EntityViewsData.php b/core/modules/views/src/EntityViewsData.php index 9a86f77..8f8055c 100644 --- a/core/modules/views/src/EntityViewsData.php +++ b/core/modules/views/src/EntityViewsData.php @@ -7,7 +7,6 @@ namespace Drupal\views; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\EntityHandlerInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -18,7 +17,6 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; -use Drupal\Core\TypedData\TypedDataManager; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -58,13 +56,6 @@ class EntityViewsData implements EntityHandlerInterface, EntityViewsDataInterfac protected $translationManager; /** - * The typed data manager. - * - * @var \Drupal\Core\TypedData\TypedDataManager - */ - protected $typedDataManager; - - /** * The field storage definitions for all base fields of the entity type. * * @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] @@ -85,13 +76,12 @@ class EntityViewsData implements EntityHandlerInterface, EntityViewsDataInterfac * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager * The translation manager. */ - function __construct(EntityTypeInterface $entity_type, SqlEntityStorageInterface $storage_controller, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager, TypedDataManager $typed_data_manager) { + function __construct(EntityTypeInterface $entity_type, SqlEntityStorageInterface $storage_controller, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) { $this->entityType = $entity_type; $this->entityManager = $entity_manager; $this->storage = $storage_controller; $this->moduleHandler = $module_handler; $this->setStringTranslation($translation_manager); - $this->typedDataManager = $typed_data_manager; } /** @@ -215,6 +205,8 @@ public function getViewsData() { /** * Puts the views data for a single field onto the views data. * + * @param string $table + * The table of the field to handle. * @param string $field_name * The name of the field to handle. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition @@ -242,10 +234,10 @@ protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterf // This would be the FID for a file reference for example. if ($first) { $first = FALSE; - $table_data[$field_name] = $this->mapSingleFieldViewsData($table, $field_name, $field_definition_type, $schema_field_name, $field_definition, TRUE); + $table_data[$field_name] = $this->mapSingleFieldViewsData($table, $field_definition_type, $schema_field_name, $field_definition, TRUE); } else { - $table_data["$field_name.$field_column_name"] = $this->mapSingleFieldViewsData($table, $field_name, $schema['type'], $schema_field_name, $field_definition, FALSE); + $table_data["$field_name.$field_column_name"] = $this->mapSingleFieldViewsData($table, $schema['type'], $schema_field_name, $field_definition, FALSE); } } } @@ -253,18 +245,21 @@ protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterf /** * Provides the views data for a given data type and schema field. * + * @param $table * @param string $data_type * The data type to generate views data for, for example "int". The data * type comes directly from the schema definition of each field item. * @param string $schema_field_name * The schema field name. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The field definition. * @param bool $first * Is it the first column of the schema. * * @return array * The modified views data field definition. */ - protected function mapSingleFieldViewsData($table, $field_name, $data_type, $schema_field_name, FieldDefinitionInterface $field_definition, $first) { + protected function mapSingleFieldViewsData($table, $data_type, $schema_field_name, FieldDefinitionInterface $field_definition, $first) { $views_field = array(); // Provide a nicer, less verbose label for the first field. @@ -355,7 +350,7 @@ protected function mapSingleFieldViewsData($table, $field_name, $data_type, $sch $process_method = 'processViewsDataFor' . Container::camelize($data_type); if (method_exists($this, $process_method)) { - $this->{$process_method}($table, $field_name, $field_definition, $views_field); + $this->{$process_method}($table, $field_definition, $views_field); } return $views_field; @@ -366,14 +361,14 @@ protected function mapSingleFieldViewsData($table, $field_name, $data_type, $sch * * @param string $table * The table the language field is added to. - * @param string $field_name - * The field name of the language field. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The field definition. * @param array $views_field * The views field data. */ - protected function processViewsDataForLanguage($table, $field_name, FieldDefinitionInterface $field_definition, array &$views_field) { + protected function processViewsDataForLanguage($table, FieldDefinitionInterface $field_definition, array &$views_field) { // Apply special titles for the langcode field. - if ($field_name == 'langcode') { + if ($field_definition->getName() == 'langcode') { if ($table == $this->entityType->getDataTable() || $table == $this->entityType->getBaseTable()) { $views_field['title'] = $this->t('Translation language'); } @@ -388,12 +383,12 @@ protected function processViewsDataForLanguage($table, $field_name, FieldDefinit * * @param string $table * The table the language field is added to. - * @param string $field_name - * The field name of the language field. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The field definition. * @param array $views_field * The views field data. */ - protected function processViewsDataForEntityReference($table, $field_name, FieldDefinitionInterface $field_definition, array &$views_field) { + protected function processViewsDataForEntityReference($table, FieldDefinitionInterface $field_definition, array &$views_field) { if ($entity_type_id = $field_definition->getItemDefinition()->getSetting('target_type')) { $entity_type = $this->entityManager->getDefinition($entity_type_id); if ($entity_type instanceof ContentEntityType) { @@ -417,7 +412,7 @@ protected function processViewsDataForEntityReference($table, $field_name, Field } } - if ($field_name == $this->entityType->getKey('bundle')) { + if ($field_definition->getName() == $this->entityType->getKey('bundle')) { // @todo Use the other bundle handlers, once // https://www.drupal.org/node/2322949 is in. $views_field['filter']['id'] = 'bundle'; diff --git a/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php b/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php index 4d6c8c8..4618309 100644 --- a/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php +++ b/core/modules/views/tests/Drupal/views/Tests/EntityViewsDataTest.php @@ -1,4 +1,5 @@ entityStorage = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityDatabaseStorage') + $this->entityStorage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage') ->disableOriginalConstructor() ->getMock(); $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); - $this->baseEntityType = new EntityType([ + $this->baseEntityType = new TestEntityType([ 'base_table' => 'entity_test', 'id' => 'entity_test', 'label' => 'Entity test', @@ -91,11 +91,8 @@ protected function setUp() { $this->translationManager = $this->getStringTranslationStub(); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); - $this->typedDataManager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManager') - ->disableOriginalConstructor() - ->getMock(); - $this->viewsData = new TestEntityViewsData($this->baseEntityType, $this->entityStorage, $this->entityManager, $this->moduleHandler, $this->translationManager, $this->typedDataManager); + $this->viewsData = new TestEntityViewsData($this->baseEntityType, $this->entityStorage, $this->entityManager, $this->moduleHandler, $this->translationManager); $field_type_manager = $this->getMockBuilder('Drupal\Core\Field\FieldTypePluginManager') ->disableOriginalConstructor() @@ -114,6 +111,22 @@ protected function setUp() { } /** + * Helper method to setup base fields. + * + * @param \Drupal\Core\Field\BaseFieldDefinition[] $base_fields + * The base fields which are adapted. + * + * @return \Drupal\Core\Field\BaseFieldDefinition[] + * The setup base fields. + */ + protected function setupBaseFields(array $base_fields) { + foreach ($base_fields as $name => $base_field) { + $base_field->setName($name); + } + return $base_fields; + } + + /** * Tests base tables. */ public function testBaseTables() { @@ -250,7 +263,7 @@ protected function setupFieldStorageDefinition() { * Tests fields on the base table. */ public function testBaseTableFields() { - $base_field_definitions = EntityTest::baseFieldDefinitions($this->baseEntityType); + $base_field_definitions = $this->setupBaseFields(EntityTest::baseFieldDefinitions($this->baseEntityType)); $this->entityManager->expects($this->once()) ->method('getBaseFieldDefinitions') ->with('entity_test') @@ -311,11 +324,12 @@ public function testDataTableFields() { ->set('id', 'entity_test_mul') ->setKey('bundle', 'type') ; - $base_field_definitions = EntityTestMul::baseFieldDefinitions($this->baseEntityType); + $base_field_definitions = $this->setupBaseFields(EntityTestMul::baseFieldDefinitions($this->baseEntityType)); $base_field_definitions['type'] = BaseFieldDefinition::create('entity_reference') ->setLabel('entity test type') ->setSettings(array('target_type' => 'entity_test_bundle')) ->setTranslatable(TRUE); + $base_field_definitions = $this->setupBaseFields($base_field_definitions); $entity_test_type = new ConfigEntityType(['id' => 'entity_test_bundle']); $user_entity_type = new ContentEntityType(['id' => 'user', 'base_table' => 'users', 'entity_keys' => ['id' => 'uid']]); $this->entityManager->expects($this->any()) @@ -404,7 +418,7 @@ public function testRevisionTableFields() { ->set('data_table', 'entity_test_mulrev_property_data') ->set('revision_data_table', 'entity_test_mulrev_property_revision') ->set('id', 'entity_test_mulrev'); - $base_field_definitions = EntityTestMulRev::baseFieldDefinitions($this->baseEntityType); + $base_field_definitions = $this->setupBaseFields(EntityTestMulRev::baseFieldDefinitions($this->baseEntityType)); $this->entityManager->expects($this->once()) ->method('getBaseFieldDefinitions') ->with('entity_test_mulrev') @@ -610,11 +624,30 @@ protected function drupalSchemaFieldsSql($table) { public function setEntityType(EntityTypeInterface $entity_type) { $this->entityType = $entity_type; } +} + +class TestEntityType extends EntityType { + + /** + * Sets a specific entity key. + * + * @param string $key + * The name of the entity key. + * @param string $value + * The new value of the key. + * + * @return $this + */ + public function setKey($key, $value) { + $this->entity_keys[$key] = $value; + return $this; + } } } + namespace { use Drupal\Component\Utility\String; diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php index 7109a62..4ff2f23 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php @@ -76,19 +76,6 @@ public function providerTestGetKeys() { } /** - * Tests the setKey() method. - * - * @covers ::setKey - */ - public function testSetKey() { - $entity_type = $this->setUpEntityType([]); - - $this->assertFalse($entity_type->getKey('id')); - $this->assertSame($entity_type, $entity_type->setKey('id', 'new_value')); - $this->assertEquals('new_value', $entity_type->getKey('id')); - } - - /** * Tests the isRevisionable() method. */ public function testIsRevisionable() {