function upload_replace_file_update in Upload File Replace (for filefield CCK) 1.0.x
Same name and namespace in other branches
- 6 upload_replace.module \upload_replace_file_update()
- 7 upload_replace.module \upload_replace_file_update()
File
- ./upload_replace.module, line 19
- A module file for providing functionality to replace files on upload
Typical Drupal behavior is to rename files on upload to <filename>_0.<ext>
This module modifies that behavior.
Code
function upload_replace_file_update(Drupal\file\FileInterface $new_file) {
if (!$new_file
->get('fid')->value) {
return;
}
$database = \Drupal::database();
$fileSystem = \Drupal::service('file_system');
$desired_destination = preg_replace('/_[0-9]+\\.(.*)$/', '.$1', $new_file
->getFileUri());
$db_path = $database
->select('file_managed', 'f')
->fields('f', array(
'uri',
))
->condition('fid', $new_file
->get('fid')->value)
->execute()
->fetchField();
if ($db_path != $new_file
->getFileUri()) {
$next_good_filepath = $fileSystem
->getDestinationFilename($desired_destination, FileSystemInterface::EXISTS_RENAME);
$database
->update('file_managed')
->fields(array(
'uri' => $next_good_filepath,
))
->condition('fid', $new_file
->get('fid')->value)
->execute();
$new_file
->setFileUri($desired_destination);
}
else {
if (!strpos($desired_destination, $new_file
->getFileUri())) {
$result = $database
->select('file_managed', 'f')
->fields('f')
->condition('uri', $desired_destination)
->execute();
$is_blocked = false;
foreach ($result as $file) {
$is_blocked = TRUE;
$blocking_file = $file;
$tmp_destination = $fileSystem
->getTempDirectory() . "/{$blocking_file->filename}";
}
$old_destination = $db_path;
if ($old_destination == $desired_destination) {
return;
}
if ($is_blocked) {
if (!$fileSystem
->move($desired_destination, $tmp_destination)) {
\Drupal::messenger()
->addMessage(t('The file %old could not be moved to %new', array(
'%old' => $desired_destination,
'%new' => $tmp_destination,
)), 'error');
return;
}
$database
->update('file_managed')
->fields(array(
'uri' => $tmp_destination,
))
->condition('fid', $blocking_file->fid)
->execute();
}
if (!$fileSystem
->move($old_destination, $desired_destination)) {
\Drupal::messenger()
->addMessage(t('The file %old could not be moved to %new', array(
'%old' => $old_destination,
'%new' => $desired_destination,
)), 'error');
return;
}
$desired_data = pathinfo($desired_destination);
$desired_filename = $desired_data['basename'];
$database
->update('file_managed')
->fields(array(
'uri' => $desired_destination,
'filename' => $desired_filename,
))
->condition('fid', $new_file
->get('fid')->value)
->execute();
$new_file
->setFileUri($desired_destination);
$new_file
->setFilename($desired_filename);
if ($is_blocked) {
if (!$fileSystem
->move($tmp_destination, $old_destination)) {
\Drupal::messenger()
->addMessage(t('The file %old could not be moved to %new', array(
'%old' => $tmp_destination,
'%new' => $old_destination,
)), 'error');
return;
}
$old_data = pathinfo($old_destination);
$old_filename = $old_data['basename'];
$database
->update('file_managed')
->fields(array(
'uri' => $old_destination,
'filename' => $old_filename,
))
->condition('fid', $blocking_file->fid)
->execute();
}
}
}
}