View source
<?php
namespace Drupal\fancy_file_delete;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Batch\BatchBuilder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\file\Entity\File;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class FancyFileDeleteBatch {
use StringTranslationTrait;
use DependencySerializationTrait;
protected $database;
protected $entityTypeManager;
protected $messenger;
protected $stringTranslation;
protected $fileSystem;
public function __construct(Connection $database, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger, TranslationInterface $string_translation, FileSystemInterface $file_system) {
$this->database = $database;
$this->entityTypeManager = $entityTypeManager;
$this->messenger = $messenger;
$this->stringTranslation = $string_translation;
$this->fileSystem = $file_system;
}
public function setBatch($values, $force, $ui = TRUE) {
$batch_builder = new BatchBuilder();
$batch_builder
->setTitle($this
->t('Deleting Files...'))
->setInitMessage($this
->t('Fun Stuff is Happening...'))
->setErrorMessage($this
->t('Fancy File Delete has encountered an error.'))
->setFinishCallback([
$this,
'finish',
]);
foreach ($values as $value) {
$batch_builder
->addOperation([
$this,
'process',
], [
$value,
$force,
]);
}
batch_set($batch_builder
->toArray());
if (!$ui) {
$batch =& batch_get();
$batch['progressive'] = FALSE;
drush_backend_batch_process();
}
}
public function process($fid, $force, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
$context['sandbox']['progress']++;
if (is_numeric($fid)) {
$file = File::load($fid);
if ($file) {
if ($force) {
$this->database
->delete('file_managed')
->condition('fid', $fid)
->execute();
$this->database
->delete('file_usage')
->condition('fid', $fid)
->execute();
$controller = $this->entityTypeManager
->getStorage('file');
$entity = $controller
->loadMultiple([
$fid,
]);
$controller
->delete($entity);
}
else {
$result = $file
->delete();
if (is_array($result)) {
$context['results']['error'][] = array(
'fid' => $fid,
'message' => $this
->t('The file with fid#%fid cannot be delete because it
is still referenced in the file_usage table. %file_usage', array(
'%fid' => $fid,
'%file_usage' => print_r($result, TRUE),
)),
);
}
else {
$context['results'][] = $fid;
}
}
}
}
else {
$this->database
->delete('unmanaged_files')
->condition('path', $fid)
->execute();
$this->fileSystem
->delete($fid);
$context['results'][] = $fid;
}
$context['message'] = $this
->t('Now cleansing the system of fid#%fid', [
'%fid' => $fid,
]);
}
public function finish($success, $results, $operations) {
if ($success) {
$this->entityTypeManager
->getStorage('file')
->resetCache();
$message = $this->stringTranslation
->formatPlural(count($results), 'One file cleansed.', '@count files cleansed.');
}
else {
$message = $this
->t('Assimilation was futile!');
}
$this->messenger
->addMessage($message);
return new RedirectResponse(Url::fromRoute('fancy_file_delete.info')
->toString());
}
}