diff --git a/core/lib/Drupal/Core/Field/FieldModuleUninstallValidator.php b/core/lib/Drupal/Core/Field/FieldModuleUninstallValidator.php index 08a27cb76f..b9ec55680e 100644 --- a/core/lib/Drupal/Core/Field/FieldModuleUninstallValidator.php +++ b/core/lib/Drupal/Core/Field/FieldModuleUninstallValidator.php @@ -46,6 +46,7 @@ public function validate($module_name) { // See \Drupal\Core\Entity\ContentUninstallValidator. if ($entity_type->getProvider() != $module_name && $entity_type->entityClassImplements(FieldableEntityInterface::class)) { foreach ($this->entityManager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) { + if ($storage_definition->getProvider() == $module_name) { $storage = $this->entityManager->getStorage($entity_type_id); if ($storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) { diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index af2fcca738..e9e8cbdc89 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -3,6 +3,7 @@ namespace Drupal\field\Entity; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\FieldableEntityStorageInterface; use Drupal\Core\Field\FieldConfigBase; use Drupal\Core\Field\FieldException; use Drupal\field\FieldStorageConfigInterface; @@ -194,8 +195,13 @@ public static function preDelete(EntityStorageInterface $storage, array $fields) // Keep the field definitions in the state storage so we can use them // later during field_purge_batch(). $deleted_fields = $state->get('field.field.deleted') ?: []; + + /** @var \Drupal\field\FieldConfigInterface $field */ foreach ($fields as $field) { - if (!$field->deleted) { + // Only mark a field for purging if there is data. Otherwise, just remove + // it. + $target_entity_storage = \Drupal::entityTypeManager()->getStorage($field->getTargetEntityTypeId()); + if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) { $config = $field->toArray(); $config['deleted'] = TRUE; $config['field_storage_uuid'] = $field->getFieldStorageDefinition()->uuid(); diff --git a/core/modules/field/src/Entity/FieldStorageConfig.php b/core/modules/field/src/Entity/FieldStorageConfig.php index 78b3d602b6..bf2641ed4e 100644 --- a/core/modules/field/src/Entity/FieldStorageConfig.php +++ b/core/modules/field/src/Entity/FieldStorageConfig.php @@ -6,6 +6,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Entity\FieldableEntityStorageInterface; use Drupal\Core\Field\FieldException; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\OptionsProviderInterface; @@ -409,8 +410,12 @@ public static function preDelete(EntityStorageInterface $storage, array $field_s // Keep the field definitions in the state storage so we can use them later // during field_purge_batch(). $deleted_storages = $state->get('field.storage.deleted') ?: []; + /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ foreach ($field_storages as $field_storage) { - if (!$field_storage->deleted) { + // Only mark a field for purging if there is data. Otherwise, just remove + // it. + $target_entity_storage = \Drupal::entityTypeManager()->getStorage($field_storage->getTargetEntityTypeId()); + if (!$field_storage->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field_storage, TRUE)) { $config = $field_storage->toArray(); $config['deleted'] = TRUE; $config['bundles'] = $field_storage->getBundles(); diff --git a/core/modules/media/media.install b/core/modules/media/media.install index 823116418c..87aeb360a0 100644 --- a/core/modules/media/media.install +++ b/core/modules/media/media.install @@ -28,15 +28,6 @@ function media_install() { } /** - * Implements hook_uninstall(). - * - * @TODO Remove when https://www.drupal.org/node/2884202 is fixed. - */ -function media_uninstall() { - \Drupal::moduleHandler()->invoke('field', 'cron'); -} - -/** * Implements hook_requirements(). */ function media_requirements($phase) { diff --git a/core/modules/system/tests/src/Functional/Module/UninstallTest.php b/core/modules/system/tests/src/Functional/Module/UninstallTest.php index 2b17b8fcf6..aa3f41dbf6 100644 --- a/core/modules/system/tests/src/Functional/Module/UninstallTest.php +++ b/core/modules/system/tests/src/Functional/Module/UninstallTest.php @@ -21,7 +21,18 @@ class UninstallTest extends BrowserTestBase { * * @var array */ - public static $modules = ['module_test', 'user', 'views', 'node']; + public static $modules = ['module_test', 'user', 'views', 'node', 'media']; + + /** + * @group failing + */ + public function testUninstallMedia() { + \Drupal::service('module_installer')->uninstall(['media']); + + \Drupal::entityManager()->clearCachedDefinitions(); + + \Drupal::service('module_installer')->uninstall(['file']); + } /** * Tests the hook_modules_uninstalled() of the user module.