Skip to content
Snippets Groups Projects
Commit b5edb5ce authored by Nikolay Grachev's avatar Nikolay Grachev
Browse files

Issue #3357873: restore check if filename already exists

(has been removed by accident).
parent 80cc232e
No related branches found
No related tags found
No related merge requests found
......@@ -112,6 +112,14 @@ class FileRenameForm extends ContentEntityForm {
$form_state->setError($form['new_filename'], $this->t('File name is invalid'));
}
}
$new_file_path = $this->getRenamedFilePath($form_state);
if (file_exists($new_file_path)) {
// File with given name already on disc.
$form_state->setError($form['new_filename'], $this->t('File %filename already exists in the same directory.', [
'%filename' => $new_filename,
]));
}
}
return parent::validateForm($form, $form_state);
......@@ -125,7 +133,7 @@ class FileRenameForm extends ContentEntityForm {
$filename_new = $form_state->getValue('new_filename') . '.' . $pathinfo['extension'];
if ($filename_new != $this->entity->getFilename()) {
$filepath_new = str_replace($this->entity->getFilename(), $filename_new, $this->entity->getFileUri());
$filepath_new = $this->getRenamedFilePath($form_state);
// Invoke pre-rename hooks.
$this->moduleHandler->invokeAll('file_prerename', [$this->entity]);
// Rename existing file.
......@@ -148,4 +156,21 @@ class FileRenameForm extends ContentEntityForm {
}
}
/**
* Get Renamed File Path.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* FileRename form state.
*
* @return string
* File name after rename.
*/
protected function getRenamedFilePath(FormStateInterface $form_state) {
$pathinfo = pathinfo($this->entity->getFileUri());
$old_filename = $pathinfo['filename'];
$new_filename = $form_state->getValue('new_filename');
// Path after renaming.
return str_replace($old_filename, $new_filename, $this->entity->getFileUri());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment