diff --git a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php index 576f9e5..12647b1 100644 --- a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php +++ b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php @@ -3,7 +3,6 @@ namespace Drupal\file\FileUsage; use Drupal\Core\Database\Connection; -use Drupal\file\Entity\File; use Drupal\file\FileInterface; /** @@ -109,23 +108,4 @@ public function listUsage(FileInterface $file) { return $references; } - /** - * {@inheritdoc} - */ - public function deleteForEntity($module, $type, $id) { - // Delete rows that have a exact or less value to prevent empty rows. - $result = $this->connection->select($this->tableName, 'f') - ->fields('f', array('fid')) - ->condition('module', $module) - ->condition('type', $type) - ->condition('id', $id) - ->execute(); - - foreach ($result as $fileRecord) { - $file = File::load($fileRecord->fid); - - $this->delete($file, $module, $type, $id); - } - } - } diff --git a/core/modules/file/src/FileUsage/FileUsageInterface.php b/core/modules/file/src/FileUsage/FileUsageInterface.php index a814e45..6acda42 100644 --- a/core/modules/file/src/FileUsage/FileUsageInterface.php +++ b/core/modules/file/src/FileUsage/FileUsageInterface.php @@ -67,17 +67,4 @@ public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $ */ public function listUsage(FileInterface $file); - /** - * Removes all relations for object. - * - * @param string $module - * The name of the module using the file. - * @param string $type - * The type of the object that contains the referenced file. - * @param int $id - * The unique, numeric ID of the object containing file references, - * that should be removed. - */ - public function deleteForEntity($module, $type, $id); - } diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php index 471a487..cd6a008 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php @@ -4,6 +4,7 @@ use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\Form\FormStateInterface; +use Drupal\file\Entity\File; /** * Represents a configurable entity file field. @@ -80,10 +81,33 @@ public function postSave($update) { public function delete() { parent::delete(); $entity = $this->getEntity(); + $entity_type = $entity->getEntityType(); - // Delete all file usages within this entity. - \Drupal::service('file.usage') - ->deleteForEntity('file', $entity->getEntityTypeId(), $entity->id()); + // Find all the files referenced by this field on all the revisions of this + // entity. + $query = \Drupal::entityQueryAggregate($entity_type->id()) + ->groupBy($this->getName()) + ->condition($entity_type->getKey('id'), $entity->id(), '='); + + if ($entity_type->isRevisionable()) { + $query->allRevisions(); + } + + // Filter by translation if we are not deleting the whole entity. + if (!$entity->isDefaultTranslation()) { + $query->aggregate($this->getName(), 'COUNT', $entity->language()->getId()); + } + + $fids = []; + foreach ($query->execute() as $row) { + if ($target_id = reset($row)) { + $fids[] = $target_id; + } + } + + foreach (File::loadMultiple($fids) as $file) { + \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), 0); + } } /** diff --git a/core/modules/file/src/Tests/FileFieldRevisionTest.php b/core/modules/file/src/Tests/FileFieldRevisionTest.php index 34b0f42..7261c74 100644 --- a/core/modules/file/src/Tests/FileFieldRevisionTest.php +++ b/core/modules/file/src/Tests/FileFieldRevisionTest.php @@ -140,9 +140,9 @@ function testRevisions() { } /** - * Testing deletion of entity with multiple revisions. + * Tests deletion of entity with multiple revisions. * - * Every entity revision referencing different file. + * Every entity revision is referencing different file. */ public function testDeleteEntityWithMultipleRevisions() { $node_storage = $this->container->get('entity.manager')->getStorage('node'); @@ -198,10 +198,10 @@ public function testDeleteEntityWithMultipleRevisions() { } /** - * Testing deletion of file field usage of it by multiple revisions entity. + * Tests deletion of file field usage for multiple revisions entity. * - * Every entity revision referencing different file, - * then after deletion of field, also all usages should be removed too. + * Every entity revision referencing a different file, then, after deleting + * the field, all usages should be removed too. */ public function testDeleteFieldWithMultipleRevisionsEntity() { $node_storage = $this->container->get('entity.manager')->getStorage('node');