function file_cron in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/file/file.module \file_cron()
Implements hook_cron().
File
- core/
modules/ file/ file.module, line 628 - Defines a "managed_file" Form API field and a "file" field for Field module.
Code
function file_cron() {
$age = \Drupal::config('system.file')
->get('temporary_maximum_age');
$file_storage = \Drupal::entityManager()
->getStorage('file');
// Only delete temporary files if older than $age. Note that automatic cleanup
// is disabled if $age set to 0.
if ($age) {
$fids = Drupal::entityQuery('file')
->condition('status', FILE_STATUS_PERMANENT, '<>')
->condition('changed', REQUEST_TIME - $age, '<')
->range(0, 100)
->execute();
$files = $file_storage
->loadMultiple($fids);
foreach ($files as $file) {
$references = \Drupal::service('file.usage')
->listUsage($file);
if (empty($references)) {
if (file_exists($file
->getFileUri())) {
$file
->delete();
}
else {
\Drupal::logger('file system')
->error('Could not delete temporary file "%path" during garbage collection', array(
'%path' => $file
->getFileUri(),
));
}
}
else {
\Drupal::logger('file system')
->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', array(
'%path' => $file
->getFileUri(),
'%modules' => implode(', ', array_keys($references)),
));
}
}
}
}