Skip to content

Commit 8d0bc44

Browse files
author
Nathaniel Catchpole
committed
Issue #3021654 by Mile23, kim.pepper, Berdir, andypost: Deprecate file_delete() and file_delete_multiple() in file.inc
1 parent e3fc5d9 commit 8d0bc44

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

core/includes/file.inc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,16 @@ function file_create_filename($basename, $directory) {
819819
* @param $fid
820820
* The file id.
821821
*
822+
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
823+
* Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead.
824+
*
822825
* @see file_unmanaged_delete()
823826
* @see \Drupal\file\FileUsage\FileUsageBase::delete()
827+
* @see \Drupal\Core\Entity\EntityStorageInterface::delete()
828+
* @see https://www.drupal.org/node/3021663
824829
*/
825830
function file_delete($fid) {
831+
@trigger_error('file_delete() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead. See https://www.drupal.org/node/3021663.', E_USER_DEPRECATED);
826832
return file_delete_multiple([$fid]);
827833
}
828834

@@ -833,14 +839,22 @@ function file_delete($fid) {
833839
* file usages instead. That will automatically mark the file as temporary and
834840
* remove it during cleanup.
835841
*
836-
* @param $fid
837-
* The file id.
842+
* @param $fids
843+
* An array of file ids.
844+
*
845+
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
846+
* Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead.
838847
*
839848
* @see file_unmanaged_delete()
840849
* @see \Drupal\file\FileUsage\FileUsageBase::delete()
850+
* @see \Drupal\Core\Entity\EntityStorageInterface::delete()
851+
* @see https://www.drupal.org/node/3021663
841852
*/
842853
function file_delete_multiple(array $fids) {
843-
entity_delete_multiple('file', $fids);
854+
@trigger_error('file_delete_multiple() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead. See https://www.drupal.org/node/3021663.', E_USER_DEPRECATED);
855+
$storage = \Drupal::entityTypeManager()->getStorage('file');
856+
$entities = $storage->loadMultiple($fids);
857+
$storage->delete($entities);
844858
}
845859

846860
/**

core/modules/file/tests/src/Kernel/FileLegacyTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,31 @@ public function testFileUrlDeprecation() {
5050
/**
5151
* @expectedDeprecation file_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\file\Entity\File::loadMultiple(). See https://www.drupal.org/node/2266845
5252
* @expectedDeprecation file_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\file\Entity\File::load(). See https://www.drupal.org/node/2266845
53+
* @expectedDeprecation file_delete() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead. See https://www.drupal.org/node/3021663.
54+
* @expectedDeprecation file_delete_multiple() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::delete() instead. See https://www.drupal.org/node/3021663.
5355
*/
5456
public function testEntityLegacyCode() {
57+
// Test deprecation of file_load_multiple().
5558
file_put_contents('public://example.txt', $this->randomMachineName());
5659
$this->assertCount(0, file_load_multiple());
5760
File::create(['uri' => 'public://example.txt'])->save();
5861
$this->assertCount(1, file_load_multiple());
5962
File::create(['uri' => 'public://example.txt'])->save();
6063
$this->assertCount(2, file_load_multiple());
64+
File::create(['uri' => 'public://example.txt'])->save();
65+
$this->assertCount(3, file_load_multiple());
6166

67+
// Test deprecation of file_load().
6268
$this->assertNull(file_load(300));
63-
$this->assertInstanceOf(FileInterface::class, file_load(1));
69+
$file_entity = file_load(1);
70+
$this->assertInstanceOf(FileInterface::class, $file_entity);
71+
72+
// Test deprecation of file_delete().
73+
$this->assertNull(file_delete($file_entity->id()));
74+
75+
// Test deprecation of file_delete_multiple().
76+
$this->assertNull(file_delete_multiple(array_keys(file_load_multiple())));
77+
$this->assertFileNotExists('public://example.txt');
6478
}
6579

6680
}

0 commit comments

Comments
 (0)