From b4f3c9335aa97580fb61a82760e597528d0187bf Mon Sep 17 00:00:00 2001 From: Nathanael Dewhurst Date: Sat, 17 Jan 2015 02:22:00 -0500 Subject: [PATCH] Issue #2409013 by ndewhurst: Updated BundleConfigImportValidate to use hasData() whenever possible. --- .../Entity/Event/BundleConfigImportValidate.php | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php b/core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php index 58ca1a2..20321e7 100644 --- a/core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php +++ b/core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php @@ -62,12 +62,24 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) { // Work out if there are entities with this bundle. $bundle_of_entity_type = $this->entityManager->getDefinition($bundle_of); $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix()); - $entity_query = $this->entityManager->getStorage($bundle_of)->getQuery(); - $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id) - ->accessCheck(FALSE) - ->range(0, 1) - ->execute(); - if (!empty($entity_ids)) { + $storage = $this->entityManager->getStorage($bundle_of); + $entities_exist = FALSE; + // Some storages are not queryable but do offer a hasData() method. + if (method_exists($storage, 'hasData')) { + $entities_exist = $storage->hasData(); + } + // Only attempt to query for entities if hasData() is not an option. + else { + $entity_query = $storage->getQuery(); + $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id) + ->accessCheck(FALSE) + ->range(0, 1) + ->execute(); + if (!empty($entity_ids)) { + $entities_exist = TRUE; + } + } + if ($entities_exist) { $entity = $this->entityManager->getStorage($entity_type_id)->load($bundle_id); $event->getConfigImporter()->logError($this->t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', array('%entity_type' => $bundle_of_entity_type->getLabel(), '%bundle_label' => $bundle_of_entity_type->getBundleLabel(), '%bundle' => $entity->label()))); }