diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index cba00ba..4a8e4fa 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -232,7 +232,9 @@ public function setTranslatable($translatable) { * {@inheritdoc} */ public function isRevisionable() { - return !empty($this->definition['revisionable']); + // Multi-valued base fields are always considered revisionable, just like + // configurable fields. + return !empty($this->definition['revisionable']) || $this->isMultiple(); } /** @@ -263,6 +265,10 @@ public function getCardinality() { * Possible values are positive integers or * FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED. * + * Note that if the entity type that this base field is attached to is + * revisionable and the field has a cardinality higher than 1, the field is + * considered revisionable by default. + * * @param int $cardinality * The field cardinality. * diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php index 9331d94..d3711f7 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionInterface.php @@ -97,7 +97,11 @@ public function isTranslatable(); public function setTranslatable($translatable); /** - * Returns whether the field is revisionable. + * Returns whether the field storage is revisionable. + * + * Note that if the entity type is revisionable and the field storage has a + * cardinality higher than 1, the field storage is considered revisionable + * by default. * * @return bool * TRUE if the field is revisionable. diff --git a/core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php index 8f945b3..3013d2a 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php @@ -19,7 +19,7 @@ class DefaultTableMappingIntegrationTest extends EntityKernelTestBase { /** * The table mapping for the tested entity type. * - * @var \Drupal\Core\Entity\Sql\TableMappingInterface + * @var \Drupal\Core\Entity\Sql\DefaultTableMapping */ protected $tableMapping; @@ -39,11 +39,18 @@ protected function setUp() { ->setName('multivalued_base_field') ->setTargetEntityTypeId('entity_test_mulrev') ->setTargetBundle('entity_test_mulrev') - ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) + // Base fields are non-translatable and non-revisionable by default, but + // we explicitly set these values here for extra clarity. + ->setTranslatable(FALSE) + ->setRevisionable(FALSE); $this->state->set('entity_test_mulrev.additional_base_field_definitions', $definitions); $this->entityManager->clearCachedDefinitions(); $this->tableMapping = $this->entityManager->getStorage('entity_test_mulrev')->getTableMapping(); + + // Ensure that the tables for the new field are created. + \Drupal::entityDefinitionUpdateManager()->applyUpdates(); } /** @@ -68,4 +75,33 @@ public function testGetFieldTableName() { $this->assertEquals($this->tableMapping->getFieldTableName('multivalued_base_field'), $expected); } + /** + * Tests DefaultTableMapping::getTableNames(). + * + * @covers ::getTableNames + */ + public function testGetTableNames() { + $storage_definitions = $this->entityManager->getFieldStorageDefinitions('entity_test_mulrev'); + $dedicated_data_table = $this->tableMapping->getDedicatedDataTableName($storage_definitions['multivalued_base_field']); + $dedicated_revision_table = $this->tableMapping->getDedicatedRevisionTableName($storage_definitions['multivalued_base_field']); + + // Check that both the data and the revision tables exist for a multi-valued + // base field. + $database_schema = \Drupal::database()->schema(); + $this->assertTrue($database_schema->tableExists($dedicated_data_table)); + $this->assertTrue($database_schema->tableExists($dedicated_revision_table)); + + // Check that the table mapping contains both the data and the revision + // tables exist for a multi-valued base field. + $expected = [ + 'entity_test_mulrev', + 'entity_test_mulrev_property_data', + 'entity_test_mulrev_revision', + 'entity_test_mulrev_property_revision', + $dedicated_data_table, + $dedicated_revision_table, + ]; + $this->assertEquals($expected, $this->tableMapping->getTableNames()); + } + }