public function FileManagementEditFileForm::submitForm in File Management 8
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ FileManagementEditFileForm.php, line 159
Class
- FileManagementEditFileForm
- Provides a form for editing files.
Namespace
Drupal\file_management\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
// TODO: Allowed extensions check. If not allowed, remove the file again!
$old_fid = $form_state
->getValue('old_fid');
$old_file = $this->entityTypeManager
->getStorage('file')
->load($old_fid);
$old_uri = $old_file
->getFileUri();
$original_uri = $old_uri;
$new_filename = $old_file
->getFilename();
$new_uri = $old_file
->getFileUri();
// Check if we have to set a new filename.
if (!empty($form_state
->getValue('new_filename'))) {
$new_filename = $form_state
->getValue('new_filename');
$new_uri = str_replace($old_file
->getFilename(), $new_filename, $new_uri);
}
// Check if we have to move the file.
if (!empty($form_state
->getValue('new_path'))) {
$new_url = parse_url($form_state
->getValue('new_path'));
$old_url = parse_url($old_uri);
$new_scheme = $old_url['scheme'];
$new_path = trim($new_url['path'], "\\/");
if (array_key_exists('scheme', $new_url)) {
$new_scheme = $new_url['scheme'];
}
$new_folder_uri = $new_scheme . '://' . $new_path;
$new_uri = $new_folder_uri . '/' . $new_filename;
if ($this->fileSystem
->prepareDirectory($new_folder_uri, FileSystemInterface::CREATE_DIRECTORY)) {
// TODO: Move the file.
}
else {
// TODO: Could not create the needed directory, ABORT!
}
}
// Check if we have to replace the file.
// TODO: set file_validate_extensions correctly
// TODO: Also check the return value of file_save_upload()[0] (could be FALSE)
$new_file = file_save_upload('new_file', [
'file_validate_extensions' => [],
]);
if (!empty($new_file) && is_array($new_file)) {
$new_file = $new_file[0];
$old_uri = $new_file
->getFileUri();
$old_file
->setMimeType($new_file
->getMimeType());
$old_file
->setSize($new_file
->getSize());
// Delete the old file.
$this->fileSystem
->delete($original_uri);
// Move the new file to where the old file was.
// TODO: We should use file_unmanaged_copy() so delete() doesn't throw a warning.
$this->fileSystem
->move($old_uri, $original_uri, FileSystemInterface::EXISTS_REPLACE);
}
if ($original_uri !== $new_uri) {
// TODO: Check if file exists - if so, abort mission [unless it's our file]
// Move the temporary uploaded file to the new location.
// TODO: We should use file_unmanaged_copy()
// So delete() doesn't throw a warning.
$this->fileSystem
->move($original_uri, $new_uri, FileSystemInterface::EXISTS_REPLACE);
}
// Delete the db entry of the new file, we've already adapted the old file.
if (!empty($new_file)) {
$new_file
->delete();
}
// Update file details.
$old_file
->setFilename($new_filename);
$old_file
->setFileUri($new_uri);
$old_file
->save();
if (!empty($new_file) && is_array($new_file)) {
// Delete the old file physically.
// this should be after the move happened AND was successful.
// TODO: don't remove if old_original_uri == new_original_uri.
$this->fileSystem
->delete($old_uri);
}
// Flush image styles.
// TODO: Flush original file.
$image_styles = ImageStyle::loadMultiple();
foreach ($image_styles as $image_style) {
$image_style
->flush($new_uri);
}
$this
->messenger()
->addMessage($this
->t('@type %title has been updated.', [
'@type' => $this
->t('File'),
'%title' => $old_file
->label(),
]));
}