diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 45246e3..1d49cd9 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -1671,7 +1671,7 @@ public function countFieldData($storage_definition, $as_bool = FALSE) { ->fields('t', array('entity_id')) ->distinct(TRUE); } - else { + elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) { $data_table = $this->dataTable ?: $this->baseTable; // @todo This check is here because system_system_info_alter() calls // countFieldData(), even in KernelTestBase tests, where the schema @@ -1697,12 +1697,17 @@ public function countFieldData($storage_definition, $as_bool = FALSE) { ->distinct(TRUE); } - // If we are performing the query just to check if the field has data - // limit the number of rows. - if ($as_bool) { - $query->range(0, 1); + // @todo Find a way to count field data also for fields having custom + // storage. See https://www.drupal.org/node/2337753. + $count = 0; + if (isset($query)) { + // If we are performing the query just to check if the field has data + // limit the number of rows. + if ($as_bool) { + $query->range(0, 1); + } + $count = $query->countQuery()->execute()->fetchField(); } - $count = $query->countQuery()->execute()->fetchField(); return $as_bool ? (bool) $count : (int) $count; } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 2b5ca0e..bd8d031 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -8,6 +8,7 @@ use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\FieldableEntityStorageInterface; use Drupal\Core\Extension\Extension; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Form\FormStateInterface; @@ -1051,13 +1052,10 @@ function system_system_info_alter(&$info, Extension $file, $type) { // We skip entity-defining modules, otherwise deleting all entities // would be required before being able to uninstall them. if ($entity_type->isFieldable() && $entity_type->getProvider() != $module_name) { - $storage_definitions = $entity_manager->getFieldStorageDefinitions($entity_type_id); - // @todo Make it easier to query if there are entities: - // https://www.drupal.org/node/2337753. - $has_data = $entity_type->hasKey('id') && $entity_manager->getStorage($entity_type_id)->countFieldData($storage_definitions[$entity_type->getKey('id')], TRUE); - if ($has_data) { - foreach ($storage_definitions as $storage_definition) { - if ($storage_definition->getProvider() == $module_name) { + $storage = $entity_manager->getStorage($entity_type_id); + if ($storage instanceof FieldableEntityStorageInterface) { + foreach ($entity_manager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) { + if ($storage_definition->getProvider() == $module_name && $storage->countFieldData($storage_definition, TRUE)) { $info['required'] = TRUE; $info['explanation'] = t('Fields type(s) in use'); break;