public function FileReplaceForm::save in File Replace (D8) 8
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ FileReplaceForm.php, line 76
Class
- FileReplaceForm
- Form controller for the file replace forms.
Namespace
Drupal\file_replace\FormCode
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\file\FileInterface $file */
$file = $this->entity;
$file_uri = $file
->getFileUri();
/** @var \Drupal\file\FileInterface $replacement */
$replacement = file_save_upload('replacement', $form['replacement']['replacement']['#upload_validators'], FALSE, 0);
if (!$replacement) {
$this
->messenger()
->addError(t('The replacement file was not saved'));
return;
}
if (!$this->fileSystem
->copy($replacement
->getFileUri(), $file_uri, FileSystemInterface::EXISTS_REPLACE)) {
$this
->messenger()
->addError(t('The file could not be replaced'));
return;
}
// Recalculate file size and change date.
$file
->save();
$this
->messenger()
->addStatus(t('The file was replaced.'));
$this->moduleHandler
->invokeAll('file_replace', [
$file,
]);
// Clean up the temporary file.
$replacement
->delete();
}