diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 832ca213e9..edf338c9ee 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -1743,10 +1743,8 @@ public function finalizePurge(FieldStorageDefinitionInterface $storage_definitio * {@inheritdoc} */ public function countFieldData($storage_definition, $as_bool = FALSE) { - // The table mapping contains stale data during a request when a field - // storage definition is added, so bypass the internal storage definitions - // and fetch the table mapping using the passed in storage definition. - // @todo Fix this in https://www.drupal.org/node/2705205. + // Ensure that the table mapping is instantiated with the passed-in field + // storage definition. $storage_definitions = $this->fieldStorageDefinitions; $storage_definitions[$storage_definition->getName()] = $storage_definition; $table_mapping = $this->getTableMapping($storage_definitions); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php index f22ab48e03..40d9488c60 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php @@ -557,6 +557,20 @@ public function testTableNames() { $this->assertEqual($this->tableMapping->getDedicatedDataTableName($field_storage, TRUE), $expected); $expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage->uuid()), 0, 10); $this->assertEqual($this->tableMapping->getDedicatedRevisionTableName($field_storage, TRUE), $expected); + + // Check that the table mapping is kept up-to-date in a request where a new + // field storage definition is added. Since the cardinality of the field is + // greater than 1, the table name retrieved from getFieldTableName() should + // be the dedicated table. + $field_storage = FieldStorageConfig::create([ + 'entity_type' => 'entity_test_rev', + 'field_name' => 'some_field_name', + 'type' => 'test_field', + 'cardinality' => 2, + ]); + $field_storage->save(); + $table_mapping = \Drupal::entityTypeManager()->getStorage('entity_test_rev')->getTableMapping(); + $this->assertEquals($table_mapping->getDedicatedDataTableName($field_storage), $table_mapping->getFieldTableName('some_field_name')); } }