diff --git a/core/modules/aggregator/src/Form/OpmlFeedAdd.php b/core/modules/aggregator/src/Form/OpmlFeedAdd.php index 74848cb..ed69762 100644 --- a/core/modules/aggregator/src/Form/OpmlFeedAdd.php +++ b/core/modules/aggregator/src/Form/OpmlFeedAdd.php @@ -115,7 +115,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $validators = array('file_validate_extensions' => array('opml xml')); - if ($file = file_save_upload('upload', $validators, FALSE, 0)) { + if ($file = file_save_upload('upload', $form_state, $validators, FALSE, 0)) { $data = file_get_contents($file->getFileUri()); } else { diff --git a/core/modules/file/file.module b/core/modules/file/file.module index fd17021..a2c128b 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -677,6 +677,8 @@ function file_cron() { * @param string $form_field_name * A string that is the associative array key of the upload form element in * the form array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. * @param array $validators * (optional) An associative array of callback functions used to validate the * file. See file_validate() for a full discussion of the array format. @@ -705,7 +707,8 @@ function file_cron() { * array element contains the file entity if the upload succeeded or FALSE if * there was an error. Function returns NULL if no file was uploaded. */ -function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) { +function file_save_upload($form_field_name, FormStateInterface $form_state, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) { + $user = \Drupal::currentUser(); static $upload_cache; @@ -739,13 +742,13 @@ function file_save_upload($form_field_name, $validators = array(), $destination switch ($file_info->getError()) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: - drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size()))), 'error'); + $form_state->setErrorByName($form_field_name, t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size())))); $files[$i] = FALSE; continue; case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: - drupal_set_message(t('The file %file could not be saved because the upload did not complete.', array('%file' => $file_info->getFilename())), 'error'); + $form_state->setErrorByName($form_field_name, t('The file %file could not be saved because the upload did not complete.', array('%file' => $file_info->getFilename()))); $files[$i] = FALSE; continue; @@ -758,7 +761,7 @@ function file_save_upload($form_field_name, $validators = array(), $destination // Unknown error default: - drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $file_info->getFilename())), 'error'); + $form_state->setErrorByName($form_field_name, t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $file_info->getFilename()))); $files[$i] = FALSE; continue; @@ -825,7 +828,7 @@ function file_save_upload($form_field_name, $validators = array(), $destination // Assert that the destination contains a valid stream. $destination_scheme = file_uri_scheme($destination); if (!file_stream_wrapper_valid_scheme($destination_scheme)) { - drupal_set_message(t('The file could not be uploaded because the destination %destination is invalid.', array('%destination' => $destination)), 'error'); + $form_state->setErrorByName($form_field_name, t('The file could not be uploaded because the destination %destination is invalid.', array('%destination' => $destination))); $files[$i] = FALSE; continue; } @@ -839,7 +842,7 @@ function file_save_upload($form_field_name, $validators = array(), $destination // If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { - drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $form_field_name, '%directory' => $destination)), 'error'); + $form_state->setErrorByName($form_field_name, t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $form_field_name, '%directory' => $destination))); $files[$i] = FALSE; continue; } @@ -873,7 +876,7 @@ function file_save_upload($form_field_name, $validators = array(), $destination // operations. $file->setFileUri($file->destination); if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) { - drupal_set_message(t('File upload error. Could not move uploaded file.'), 'error'); + $form_state->setErrorByName($form_field_name, t('File upload error. Could not move uploaded file.')); \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->getFilename(), '%destination' => $file->getFileUri())); $files[$i] = FALSE; continue; @@ -1184,7 +1187,7 @@ function file_managed_file_save_upload($element, FormStateInterface $form_state) $files_uploaded = $element['#multiple'] && count(array_filter($file_upload)) > 0; $files_uploaded |= !$element['#multiple'] && !empty($file_upload); if ($files_uploaded) { - if (!$files = file_save_upload($upload_name, $element['#upload_validators'], $destination)) { + if (!$files = file_save_upload($upload_name, $form_state, $element['#upload_validators'], $destination)) { \Drupal::logger('file')->notice('The file upload failed. %upload', array('%upload' => $upload_name)); $form_state->setError($element, t('Files in the @name field were unable to be uploaded.', array('@name' => $element['#title']))); return array(); diff --git a/core/modules/file/tests/file_test/src/Form/FileTestForm.php b/core/modules/file/tests/file_test/src/Form/FileTestForm.php index f903bf0..ea25c85 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestForm.php @@ -109,7 +109,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { define('SIMPLETEST_COLLECT_ERRORS', FALSE); } - $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state->getValue('file_test_replace')); + $file = file_save_upload('file_test_upload', $form_state, $validators, $destination, 0, $form_state->getValue('file_test_replace')); if ($file) { $form_state->setValue('file_test_upload', $file); drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->getFileUri()))); diff --git a/core/modules/locale/src/Form/ImportForm.php b/core/modules/locale/src/Form/ImportForm.php index f3007f6..7cc6cf8 100644 --- a/core/modules/locale/src/Form/ImportForm.php +++ b/core/modules/locale/src/Form/ImportForm.php @@ -159,7 +159,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - $this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0); + $this->file = file_save_upload('file', $form_state, $form['file']['#upload_validators'], 'translations://', 0); // Ensure we have the file uploaded. if (!$this->file) { diff --git a/core/modules/system/src/Form/ThemeSettingsForm.php b/core/modules/system/src/Form/ThemeSettingsForm.php index 82ad415..2d1f534 100644 --- a/core/modules/system/src/Form/ThemeSettingsForm.php +++ b/core/modules/system/src/Form/ThemeSettingsForm.php @@ -355,7 +355,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $validators = array('file_validate_is_image' => array()); // Check for a new uploaded logo. - $file = file_save_upload('logo_upload', $validators, FALSE, 0); + $file = file_save_upload('logo_upload', $form_state, $validators, FALSE, 0); if (isset($file)) { // File upload was attempted. if ($file) { @@ -371,7 +371,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $validators = array('file_validate_extensions' => array('ico png gif jpg jpeg apng svg')); // Check for a new uploaded favicon. - $file = file_save_upload('favicon_upload', $validators, FALSE, 0); + $file = file_save_upload('favicon_upload', $form_state, $validators, FALSE, 0); if (isset($file)) { // File upload was attempted. if ($file) { diff --git a/core/modules/update/src/Form/UpdateManagerInstall.php b/core/modules/update/src/Form/UpdateManagerInstall.php index 28e0507..2b1dddb 100644 --- a/core/modules/update/src/Form/UpdateManagerInstall.php +++ b/core/modules/update/src/Form/UpdateManagerInstall.php @@ -147,7 +147,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } elseif ($_FILES['files']['name']['project_upload']) { $validators = array('file_validate_extensions' => array(archiver_get_extensions())); - if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) { + if (!($finfo = file_save_upload('project_upload', $form_state, $validators, NULL, 0, FILE_EXISTS_REPLACE))) { // Failed to upload the file. file_save_upload() calls // drupal_set_message() on failure. return;