diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 1ce4298..5832f12 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -13,6 +13,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\file\Entity\File; +use Drupal\file\FileFormTrait; use Drupal\file\FileInterface; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Unicode; @@ -674,81 +675,6 @@ function file_cron() { } /** - * Saves form file uploads. - * - * The files will be added to the {file_managed} table as temporary files. - * Temporary files are periodically cleaned. Use the 'file.usage' service to - * register the usage of the file which will automatically mark it as permanent. - * - * @param array $element - * The FAPI element whose values are being saved. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * @param null|int $delta - * (optional) The delta of the file to return the file entity. - * Defaults to NULL. - * @param int $replace - * (optional) The replace behavior when the destination file already exists. - * Possible values include: - * - FILE_EXISTS_REPLACE: Replace the existing file. - * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the - * filename is unique. - * - FILE_EXISTS_ERROR: Do nothing and return FALSE. - * - * @return array|\Drupal\file\FileInterface|null|false - * An array of file entities or a single file entity if $delta != NULL. Each - * 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_from_form($element, FormStateInterface $form_state, $delta = NULL, $replace = FILE_EXISTS_RENAME) { - // Get all errors set before calling this method. This will also clear them - // from $_SESSION. - $errors_before = drupal_get_messages('error'); - - $upload_location = isset($element['#upload_location']) ? $element['#upload_location'] : FALSE; - $upload_name = implode('_', $element['#parents']); - $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : []; - - $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $replace); - - // Get new errors that are generated while trying to save the upload. This - // will also clear them from $_SESSION. - $errors_new = drupal_get_messages('error'); - if (!empty($errors_new['error'])) { - $errors_new = $errors_new['error']; - - if (count($errors_new) > 1) { - // Render multiple errors into a single message. - $render_array = [ - 'error' => [ - '#markup' => t('One or more files could not be uploaded.'), - ], - 'item_list' => [ - '#theme' => 'item_list', - '#items' => $errors_new, - ], - ]; - $error_message = \Drupal::service('renderer')->renderPlain($render_array); - } - else { - $error_message = reset($errors_new); - } - - $form_state->setError($element, $error_message); - } - - // Ensure that errors set prior to calling this method are still shown to the - // user. - if (!empty($errors_before['error'])) { - foreach ($errors_before['error'] as $error) { - drupal_set_message($error, 'error'); - } - } - - return $result; -} - -/** * Saves file uploads to a new location. * * The files will be added to the {file_managed} table as temporary files. @@ -788,209 +714,17 @@ function file_save_upload_from_form($element, FormStateInterface $form_state, $d * * @deprecated in Drupal 8.3.x, will be removed before Drupal 9.0.0. * This function should not be used for form validation, use - * file_save_upload_from_form() instead. + * \Drupal\file\FileFormTrait::fileSaveUpload() instead. * - * @see file_save_upload_from_form() + * @see \Drupal\file\FileFormTrait::fileSaveUpload() */ function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) { - $user = \Drupal::currentUser(); - static $upload_cache; - - $all_files = \Drupal::request()->files->get('files', array()); - // Make sure there's an upload to process. - if (empty($all_files[$form_field_name])) { - return NULL; - } - $file_upload = $all_files[$form_field_name]; - - // Return cached objects without processing since the file will have - // already been processed and the paths in $_FILES will be invalid. - if (isset($upload_cache[$form_field_name])) { - if (isset($delta)) { - return $upload_cache[$form_field_name][$delta]; - } - return $upload_cache[$form_field_name]; - } - - // Prepare uploaded files info. Representation is slightly different - // for multiple uploads and we fix that here. - $uploaded_files = $file_upload; - if (!is_array($file_upload)) { - $uploaded_files = array($file_upload); + $errors = []; + $files = FileFormTrait::_fileSaveUpload($form_field_name, $validators, $destination, $delta, $replace, $errors); + foreach ($errors as $error) { + drupal_set_message($error, 'error'); } - - $files = array(); - foreach ($uploaded_files as $i => $file_info) { - // Check for file upload errors and return FALSE for this file if a lower - // level system error occurred. For a complete list of errors: - // See http://php.net/manual/features.file-upload.errors.php. - 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'); - $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'); - $files[$i] = FALSE; - continue; - - case UPLOAD_ERR_OK: - // Final check that this is a valid upload, if it isn't, use the - // default error handler. - if (is_uploaded_file($file_info->getRealPath())) { - break; - } - - // 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'); - $files[$i] = FALSE; - continue; - - } - // Begin building file entity. - $values = array( - 'uid' => $user->id(), - 'status' => 0, - 'filename' => $file_info->getClientOriginalName(), - 'uri' => $file_info->getRealPath(), - 'filesize' => $file_info->getSize(), - ); - $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['filename']); - $file = File::create($values); - - $extensions = ''; - if (isset($validators['file_validate_extensions'])) { - if (isset($validators['file_validate_extensions'][0])) { - // Build the list of non-munged extensions if the caller provided them. - $extensions = $validators['file_validate_extensions'][0]; - } - else { - // If 'file_validate_extensions' is set and the list is empty then the - // caller wants to allow any extension. In this case we have to remove the - // validator or else it will reject all extensions. - unset($validators['file_validate_extensions']); - } - } - else { - // No validator was provided, so add one using the default list. - // Build a default non-munged safe list for file_munge_filename(). - $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'; - $validators['file_validate_extensions'] = array(); - $validators['file_validate_extensions'][0] = $extensions; - } - - if (!empty($extensions)) { - // Munge the filename to protect against possible malicious extension - // hiding within an unknown file type (ie: filename.html.foo). - $file->setFilename(file_munge_filename($file->getFilename(), $extensions)); - } - - // Rename potentially executable files, to help prevent exploits (i.e. will - // rename filename.php.foo and filename.php to filename.php.foo.txt and - // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads' - // evaluates to TRUE. - if (!\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->getFilename()) && (substr($file->getFilename(), -4) != '.txt')) { - $file->setMimeType('text/plain'); - // The destination filename will also later be used to create the URI. - $file->setFilename($file->getFilename() . '.txt'); - // The .txt extension may not be in the allowed list of extensions. We have - // to add it here or else the file upload will fail. - if (!empty($extensions)) { - $validators['file_validate_extensions'][0] .= ' txt'; - drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array('%filename' => $file->getFilename()))); - } - } - - // If the destination is not provided, use the temporary directory. - if (empty($destination)) { - $destination = 'temporary://'; - } - - // 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'); - $files[$i] = FALSE; - continue; - } - - $file->source = $form_field_name; - // A file URI may already have a trailing slash or look like "public://". - if (substr($destination, -1) != '/') { - $destination .= '/'; - } - $file->destination = file_destination($destination . $file->getFilename(), $replace); - // 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'); - $files[$i] = FALSE; - continue; - } - - // Add in our check of the file name length. - $validators['file_validate_name_length'] = array(); - - // Call the validation functions specified by this function's caller. - $errors = file_validate($file, $validators); - - // Check for errors. - if (!empty($errors)) { - $message = array( - 'error' => array( - '#markup' => t('The specified file %name could not be uploaded.', array('%name' => $file->getFilename())), - ), - 'item_list' => array( - '#theme' => 'item_list', - '#items' => $errors, - ), - ); - // @todo Add support for render arrays in drupal_set_message()? See - // https://www.drupal.org/node/2505497. - drupal_set_message(\Drupal::service('renderer')->renderPlain($message), 'error'); - $files[$i] = FALSE; - continue; - } - - // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary - // directory. This overcomes open_basedir restrictions for future file - // 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'); - \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; - } - - // Set the permissions on the new file. - drupal_chmod($file->getFileUri()); - - // If we are replacing an existing file re-use its database record. - // @todo Do not create a new entity in order to update it. See - // https://www.drupal.org/node/2241865. - if ($replace == FILE_EXISTS_REPLACE) { - $existing_files = entity_load_multiple_by_properties('file', array('uri' => $file->getFileUri())); - if (count($existing_files)) { - $existing = reset($existing_files); - $file->fid = $existing->id(); - $file->setOriginalId($existing->id()); - } - } - - // If we made it this far it's safe to record this file in the database. - $file->save(); - $files[$i] = $file; - } - - // Add files to the cache. - $upload_cache[$form_field_name] = $files; - - return isset($delta) ? $files[$delta] : $files; + return $files; } /** @@ -1273,7 +1007,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_from_form($element, $form_state)) { + if (!$files = FileFormTrait::fileSaveUpload($element, $form_state)) { \Drupal::logger('file')->notice('The file upload failed. %upload', array('%upload' => $upload_name)); return array(); } diff --git a/core/modules/file/src/FileFormTrait.php b/core/modules/file/src/FileFormTrait.php new file mode 100644 index 0000000..1554e84 --- /dev/null +++ b/core/modules/file/src/FileFormTrait.php @@ -0,0 +1,317 @@ + 1) { + // Render multiple errors into a single message. + $render_array = [ + 'error' => [ + '#markup' => t('One or more files could not be uploaded.'), + ], + 'item_list' => [ + '#theme' => 'item_list', + '#items' => $errors, + ], + ]; + $error_message = \Drupal::service('renderer')->renderPlain($render_array); + } + else { + $error_message = reset($errors); + } + $form_state->setError($element, $error_message); + } + + return $files; + } + + /** + * Saves file uploads to a new location. + * + * The files will be added to the {file_managed} table as temporary files. + * Temporary files are periodically cleaned. Use the 'file.usage' service to + * register the usage of the file which will automatically mark it as permanent. + * + * @param string $form_field_name + * A string that is the associative array key of the upload form element in + * the form array. + * @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. + * If the array is empty, it will be set up to call file_validate_extensions() + * with a safe list of extensions, as follows: "jpg jpeg gif png txt doc + * xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly + * set this array to ['file_validate_extensions' => '']. (Beware: this is not + * safe and should only be allowed for trusted users, if at all.) + * @param string|false $destination + * (optional) A string containing the URI that the file should be copied to. + * This must be a stream wrapper URI. If this value is omitted or set to + * FALSE, Drupal's temporary files scheme will be used ("temporary://"). + * @param null|int $delta + * (optional) The delta of the file to return the file entity. + * Defaults to NULL. + * @param int $replace + * (optional) The replace behavior when the destination file already exists. + * Possible values include: + * - FILE_EXISTS_REPLACE: Replace the existing file. + * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the + * filename is unique. + * - FILE_EXISTS_ERROR: Do nothing and return FALSE. + * @param array $errors_to_return + * (optional) Any errors that have occurred whilst uploading the file. + * + * @return array|\Drupal\file\FileInterface|null|false + * An array of file entities or a single file entity if $delta != NULL. Each + * 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. + * + * @see file_save_upload() + * @see file_save_upload() + * + * @internal + */ + public static function _fileSaveUpload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME, &$errors_to_return = []) { + $user = \Drupal::currentUser(); + static $upload_cache; + + $all_files = \Drupal::request()->files->get('files', []); + // Make sure there's an upload to process. + if (empty($all_files[$form_field_name])) { + return NULL; + } + $file_upload = $all_files[$form_field_name]; + + // Return cached objects without processing since the file will have + // already been processed and the paths in $_FILES will be invalid. + if (isset($upload_cache[$form_field_name])) { + if (isset($delta)) { + return $upload_cache[$form_field_name][$delta]; + } + return $upload_cache[$form_field_name]; + } + + // Prepare uploaded files info. Representation is slightly different + // for multiple uploads and we fix that here. + $uploaded_files = $file_upload; + if (!is_array($file_upload)) { + $uploaded_files = array($file_upload); + } + + $files = array(); + foreach ($uploaded_files as $i => $file_info) { + // Check for file upload errors and return FALSE for this file if a lower + // level system error occurred. For a complete list of errors: + // See http://php.net/manual/features.file-upload.errors.php. + switch ($file_info->getError()) { + case UPLOAD_ERR_INI_SIZE: + case UPLOAD_ERR_FORM_SIZE: + $errors_to_return[] = new TranslatableMarkup('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size())]); + $files[$i] = FALSE; + break; + + case UPLOAD_ERR_PARTIAL: + case UPLOAD_ERR_NO_FILE: + $errors_to_return[] = new TranslatableMarkup('The file %file could not be saved because the upload did not complete.', ['%file' => $file_info->getFilename()]); + $files[$i] = FALSE; + break; + + case UPLOAD_ERR_OK: + // Final check that this is a valid upload, if it isn't, use the + // default error handler. + if (is_uploaded_file($file_info->getRealPath())) { + break; + } + + default: + // Unknown error. + $errors_to_return[] = new TranslatableMarkup('The file %file could not be saved. An unknown error has occurred.', ['%file' => $file_info->getFilename()]); + $files[$i] = FALSE; + break; + + } + // Begin building file entity. + $values = [ + 'uid' => $user->id(), + 'status' => 0, + 'filename' => $file_info->getClientOriginalName(), + 'uri' => $file_info->getRealPath(), + 'filesize' => $file_info->getSize(), + ]; + $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['filename']); + $file = File::create($values); + + $extensions = ''; + if (isset($validators['file_validate_extensions'])) { + if (isset($validators['file_validate_extensions'][0])) { + // Build the list of non-munged extensions if the caller provided them. + $extensions = $validators['file_validate_extensions'][0]; + } + else { + // If 'file_validate_extensions' is set and the list is empty then the + // caller wants to allow any extension. In this case we have to remove the + // validator or else it will reject all extensions. + unset($validators['file_validate_extensions']); + } + } + else { + // No validator was provided, so add one using the default list. + // Build a default non-munged safe list for file_munge_filename(). + $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'; + $validators['file_validate_extensions'] = array(); + $validators['file_validate_extensions'][0] = $extensions; + } + + if (!empty($extensions)) { + // Munge the filename to protect against possible malicious extension + // hiding within an unknown file type (ie: filename.html.foo). + $file->setFilename(file_munge_filename($file->getFilename(), $extensions)); + } + + // Rename potentially executable files, to help prevent exploits (i.e. will + // rename filename.php.foo and filename.php to filename.php.foo.txt and + // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads' + // evaluates to TRUE. + if (!\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match('/\.(php|pl|py|cgi|asp|js)(\.|$)/i', $file->getFilename()) && (substr($file->getFilename(), -4) != '.txt')) { + $file->setMimeType('text/plain'); + // The destination filename will also later be used to create the URI. + $file->setFilename($file->getFilename() . '.txt'); + // The .txt extension may not be in the allowed list of extensions. We have + // to add it here or else the file upload will fail. + if (!empty($extensions)) { + $validators['file_validate_extensions'][0] .= ' txt'; + drupal_set_message(new TranslatableMarkup('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()])); + } + } + + // If the destination is not provided, use the temporary directory. + if (empty($destination)) { + $destination = 'temporary://'; + } + + // Assert that the destination contains a valid stream. + $destination_scheme = file_uri_scheme($destination); + if (!file_stream_wrapper_valid_scheme($destination_scheme)) { + $errors_to_return[] = new TranslatableMarkup('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]); + $files[$i] = FALSE; + continue; + } + + $file->source = $form_field_name; + // A file URI may already have a trailing slash or look like "public://". + if (substr($destination, -1) != '/') { + $destination .= '/'; + } + $file->destination = file_destination($destination . $file->getFilename(), $replace); + // 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) { + $errors_to_return[] = new TranslatableMarkup('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', ['%source' => $form_field_name, '%directory' => $destination]); + $files[$i] = FALSE; + continue; + } + + // Add in our check of the file name length. + $validators['file_validate_name_length'] = array(); + + // Call the validation functions specified by this function's caller. + $errors = file_validate($file, $validators); + + // Check for errors. + if (!empty($errors)) { + $message = array( + 'error' => array( + '#markup' => t('The specified file %name could not be uploaded.', array('%name' => $file->getFilename())), + ), + 'item_list' => array( + '#theme' => 'item_list', + '#items' => $errors, + ), + ); + // @todo Add support for render arrays in drupal_set_message()? See + // https://www.drupal.org/node/2505497. + $errors_to_return[] = \Drupal::service('renderer')->renderPlain($message); + $files[$i] = FALSE; + continue; + } + + // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary + // directory. This overcomes open_basedir restrictions for future file + // operations. + $file->setFileUri($file->destination); + if (!\Drupal::service('file_system')->moveUploadedFile($file_info->getRealPath(), $file->getFileUri())) { + $errors_to_return[] = new TranslatableMarkup('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; + } + + // Set the permissions on the new file. + drupal_chmod($file->getFileUri()); + + // If we are replacing an existing file re-use its database record. + // @todo Do not create a new entity in order to update it. See + // https://www.drupal.org/node/2241865. + if ($replace == FILE_EXISTS_REPLACE) { + $existing_files = entity_load_multiple_by_properties('file', array('uri' => $file->getFileUri())); + if (count($existing_files)) { + $existing = reset($existing_files); + $file->fid = $existing->id(); + $file->setOriginalId($existing->id()); + } + } + + // If we made it this far it's safe to record this file in the database. + $file->save(); + $files[$i] = $file; + } + + // Add files to the cache. + $upload_cache[$form_field_name] = $files; + + return isset($delta) ? $files[$delta] : $files; + } + +} diff --git a/core/modules/file/src/Tests/SaveUploadFormTest.php b/core/modules/file/src/Tests/SaveUploadFormTest.php index c795120..3714b97 100644 --- a/core/modules/file/src/Tests/SaveUploadFormTest.php +++ b/core/modules/file/src/Tests/SaveUploadFormTest.php @@ -5,11 +5,11 @@ use Drupal\file\Entity\File; /** - * Tests the file_save_upload_from_form() function. + * Tests the \Drupal\file\FileFormTrait::fileSaveUpload() function. * * @group file * - * @see file_save_upload_from_form() + * @see \Drupal\file\FileFormTrait::fileSaveUpload() */ class SaveUploadFormTest extends FileManagedTestBase { diff --git a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php index d06f53f..0edf5a2 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestSaveUploadFromForm.php @@ -5,12 +5,14 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\State\StateInterface; +use Drupal\file\FileFormTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** * File test form class. */ class FileTestSaveUploadFromForm extends FormBase { + use FileFormTrait; /** * Stores the state storage service. @@ -144,7 +146,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $form['file_test_upload']['#upload_location'] = $destination; drupal_set_message($this->t('Number of error messages before file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))])); - $file = file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace')); + $file = $this->fileSaveUpload($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace')); drupal_set_message($this->t('Number of error messages after file_save_upload_from_form(): @count.', ['@count' => count(drupal_get_messages('error', FALSE))])); if ($file) { diff --git a/core/modules/locale/src/Form/ImportForm.php b/core/modules/locale/src/Form/ImportForm.php index 60b2d50..f344873 100644 --- a/core/modules/locale/src/Form/ImportForm.php +++ b/core/modules/locale/src/Form/ImportForm.php @@ -5,6 +5,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\file\FileFormTrait; use Drupal\language\ConfigurableLanguageManagerInterface; use Drupal\language\Entity\ConfigurableLanguage; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -13,6 +14,7 @@ * Form constructor for the translation import screen. */ class ImportForm extends FormBase { + use FileFormTrait; /** * Uploaded file entity. @@ -155,7 +157,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - $this->file = file_save_upload_from_form($form['file'], $form_state, 0); + $this->file = $this->fileSaveUpload($form['file'], $form_state, 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 5dbc411..43cbc8d 100644 --- a/core/modules/system/src/Form/ThemeSettingsForm.php +++ b/core/modules/system/src/Form/ThemeSettingsForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\Core\StreamWrapper\PublicStream; +use Drupal\file\FileFormTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -364,14 +365,14 @@ public function validateForm(array &$form, FormStateInterface $form_state) { if ($this->moduleHandler->moduleExists('file')) { // Check for a new uploaded logo. - $file = file_save_upload_from_form($form['logo']['settings']['logo_upload'], $form_state, 0); + $file = FileFormTrait::fileSaveUpload($form['logo']['settings']['logo_upload'], $form_state, 0); if ($file) { // Put the temporary file in form_values so we can save it on submit. $form_state->setValue('logo_upload', $file); } // Check for a new uploaded favicon. - $file = file_save_upload_from_form($form['favicon']['settings']['favicon_upload'], $form_state, 0); + $file = FileFormTrait::fileSaveUpload($form['favicon']['settings']['favicon_upload'], $form_state, 0); if ($file) { // Put the temporary file in form_values so we can save it on submit. $form_state->setValue('favicon_upload', $file);