diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index f132d89489b87cee3bac0f03d88253965b8de7b6..31bf8edaf730a2599a9c2850df1f5331adf5e8aa 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -351,20 +351,26 @@ public function delete(array $entities) { return; } + // Ensure that the entities are keyed by ID. + $keyed_entities = []; + foreach ($entities as $entity) { + $keyed_entities[$entity->id()] = $entity; + } + // Allow code to run before deleting. $entity_class = $this->entityClass; - $entity_class::preDelete($this, $entities); - foreach ($entities as $entity) { + $entity_class::preDelete($this, $keyed_entities); + foreach ($keyed_entities as $entity) { $this->invokeHook('predelete', $entity); } // Perform the delete and reset the static cache for the deleted entities. - $this->doDelete($entities); - $this->resetCache(array_keys($entities)); + $this->doDelete($keyed_entities); + $this->resetCache(array_keys($keyed_entities)); // Allow code to run after deleting. - $entity_class::postDelete($this, $entities); - foreach ($entities as $entity) { + $entity_class::postDelete($this, $keyed_entities); + foreach ($keyed_entities as $entity) { $this->invokeHook('delete', $entity); } } diff --git a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php index 5bb10492237a17451a586bec23bbfdb4193fef10..890a9b42f6813c5e1c02f08df06aad35a2b1389e 100644 --- a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php @@ -146,10 +146,7 @@ public function deleteRevision($revision_id) { * {@inheritdoc} */ public function doDelete($entities) { - $entity_ids = array(); - foreach ($entities as $entity) { - $entity_ids[] = $entity->id(); - } + $entity_ids = array_keys($entities); $this->keyValueStore->deleteMultiple($entity_ids); } diff --git a/core/modules/system/src/Tests/Entity/EntityApiTest.php b/core/modules/system/src/Tests/Entity/EntityApiTest.php index f337088ea1901e78fb6de4a35a0f2db5e27096bd..6176870736f4727ecae3e6835e9181d2075a3533 100644 --- a/core/modules/system/src/Tests/Entity/EntityApiTest.php +++ b/core/modules/system/src/Tests/Entity/EntityApiTest.php @@ -95,6 +95,31 @@ protected function assertCRUD($entity_type, UserInterface $user1) { if ($revision_table = $definition->getRevisionTable()) { $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied'); } + + // Test deleting a list of entities not indexed by entity id. + $entities = array(); + $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->id())); + $entity->save(); + $entities['test'] = $entity; + $entity = entity_create($entity_type, array('name' => 'test2', 'user_id' => $user1->id())); + $entity->save(); + $entities['test2'] = $entity; + $controller = \Drupal::entityManager()->getStorage($entity_type); + $controller->delete($entities); + + // Verify that entities got deleted. + $all = entity_load_multiple($entity_type); + $this->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', array('%entity_type' => $entity_type))); + + // Verify that all data got deleted from the tables. + $definition = \Drupal::entityManager()->getDefinition($entity_type); + $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied'); + if ($data_table = $definition->getDataTable()) { + $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied'); + } + if ($revision_table = $definition->getRevisionTable()) { + $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied'); + } } /**