public function ServiceAuditFilesMergeFileReferences::auditfilesMergeFileReferencesBatchMergeProcessFile in Audit Files 8.2
Same name and namespace in other branches
- 8.3 src/ServiceAuditFilesMergeFileReferences.php \Drupal\auditfiles\ServiceAuditFilesMergeFileReferences::auditfilesMergeFileReferencesBatchMergeProcessFile()
Deletes the specified file from the file_managed table.
Parameters
int $file_being_kept: The file ID of the file to merge the other into.
int $file_being_merged: The file ID of the file to merge.
File
- src/
ServiceAuditFilesMergeFileReferences.php, line 231
Class
- ServiceAuditFilesMergeFileReferences
- Define all methods that used in merge file references functionality.
Namespace
Drupal\auditfilesCode
public function auditfilesMergeFileReferencesBatchMergeProcessFile($file_being_kept, $file_being_merged) {
$connection = $this->connection;
$file_being_kept_results = $connection
->select('file_usage', 'fu')
->fields('fu', [
'module',
'type',
'id',
'count',
])
->condition('fid', $file_being_kept)
->execute()
->fetchAll();
if (empty($file_being_kept_results)) {
$this
->messenger()
->addWarning($this->stringTranslation
->translate('There was no file usage data found for the file you choose to keep. No changes were made.'));
return;
}
$file_being_kept_data = reset($file_being_kept_results);
$file_being_merged_results = $connection
->select('file_usage', 'fu')
->fields('fu', [
'module',
'type',
'id',
'count',
])
->condition('fid', $file_being_merged)
->execute()
->fetchAll();
if (empty($file_being_merged_results)) {
$this
->messenger()
->addWarning($this->stringTranslation
->translate('There was an error retrieving the file usage data from the database for file ID %fid. Please check the files in one of the other reports. No changes were made for this file.', [
'%fid' => $file_being_merged,
]));
return;
}
$file_being_merged_data = reset($file_being_merged_results);
$file_being_merged_uri_results = $connection
->select('file_managed', 'fm')
->fields('fm', [
'uri',
])
->condition('fid', $file_being_merged)
->execute()
->fetchAll();
$file_being_merged_uri = reset($file_being_merged_uri_results);
if ($file_being_kept_data->id == $file_being_merged_data->id) {
$file_being_kept_data->count += $file_being_merged_data->count;
// Delete the unnecessary entry from the file_usage table.
$connection
->delete('file_usage')
->condition('fid', $file_being_merged)
->execute();
// Update the entry for the file being kept.
$connection
->update('file_usage')
->fields([
'count' => $file_being_kept_data->count,
])
->condition('fid', $file_being_kept)
->condition('module', $file_being_kept_data->module)
->condition('type', $file_being_kept_data->type)
->condition('id', $file_being_kept_data->id)
->execute();
}
else {
$connection
->update('file_usage')
->fields([
'fid' => $file_being_kept,
])
->condition('fid', $file_being_merged)
->condition('module', $file_being_merged_data->module)
->condition('type', $file_being_merged_data->type)
->condition('id', $file_being_merged_data->id)
->execute();
// Update any fields that might be pointing to the file being merged.
$this
->auditfilesMergeFileReferencesUpdateFileFields($file_being_kept, $file_being_merged);
}
// Delete the unnecessary entries from the file_managed table.
$connection
->delete('file_managed')
->condition('fid', $file_being_merged)
->execute();
// Delete the duplicate file.
if (!empty($file_being_merged_uri->uri)) {
$this->fileSystem
->delete($file_being_merged_uri->uri);
}
}