diff --git a/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php b/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php index ac76d9a751bdc3c942973f8722a63a7de523a358..86ea4f5755897781f65e933b2ad296de1fe17375 100644 --- a/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php +++ b/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php @@ -40,12 +40,20 @@ public function validate($module) { $entity_types = $this->entityTypeManager->getDefinitions(); $reasons = []; foreach ($entity_types as $entity_type) { - if ($module == $entity_type->getProvider() && $entity_type instanceof ContentEntityTypeInterface && $this->entityTypeManager->getStorage($entity_type->id())->hasData()) { - $reasons[] = $this->t('There is content for the entity type: @entity_type. <a href=":url">Remove @entity_type_plural</a>.', [ - '@entity_type' => $entity_type->getLabel(), - '@entity_type_plural' => $entity_type->getPluralLabel(), - ':url' => Url::fromRoute('system.prepare_modules_entity_uninstall', ['entity_type_id' => $entity_type->id()])->toString(), - ]); + try { + if ($module == $entity_type->getProvider() && $entity_type instanceof ContentEntityTypeInterface && $this->entityTypeManager->getStorage($entity_type->id())->hasData()) { + $reasons[] = $this->t('There is content for the entity type: @entity_type. <a href=":url">Remove @entity_type_plural</a>.', [ + '@entity_type' => $entity_type->getLabel(), + '@entity_type_plural' => $entity_type->getPluralLabel(), + ':url' => Url::fromRoute('system.prepare_modules_entity_uninstall', ['entity_type_id' => $entity_type->id()])->toString(), + ]); + } + } + catch (\Throwable $th) { + // An exception here means that the table does not exist. That means + // there is no data. This exception is caught to allow to uninstall + // modules with incorrectly installed entity types. + return $reasons = []; } } return $reasons; diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php index ad4f88177a0428872868a2239c177d3e24971324..9e02f9f9a4619b705368e49f9447d389d389db60 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php @@ -430,4 +430,15 @@ public function testIdentifierSchema() { $this->assertEquals($expected_revision_id_schema, $revision_id_schema); } + /** + * Manually drop one of the test entity type tables to simulate a scenario + * where a table was not created or an entity type is new. + */ + public function testUninstall() { + $this->installModule('entity_test'); + \Drupal::database()->schema()->dropTable('entity_test'); + // Uninstall the entity_test module. + $this->container->get('module_installer')->uninstall(['entity_test']); + } + }