diff --git a/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php b/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php
index ac76d9a751..86ea4f5755 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. Remove @entity_type_plural.', [
- '@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. Remove @entity_type_plural.', [
+ '@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 12265ab9c8..dd5d3ac408 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php
@@ -443,4 +443,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']);
+ }
+
}