Commit 3ea2ca57 authored by catch's avatar catch
Browse files

Issue #3156949 by ytsurk, Berdir: Filename is not shown in the maximum allowed...

Issue #3156949 by ytsurk, Berdir: Filename is not shown in the maximum allowed file size error message (w/o using the file_validate_size upload validator)

(cherry picked from commit 70892a69)
parent 78cbebb1
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -961,18 +961,19 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
 */
function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $validators = [], $destination = FALSE, $replace = FileSystemInterface::EXISTS_REPLACE) {
  $user = \Drupal::currentUser();
  $original_file_name = trim($file_info->getClientOriginalName(), '.');
  // 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::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => format_size(Environment::getUploadMaxSize())]));
      \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $original_file_name, '%maxsize' => format_size(Environment::getUploadMaxSize())]));
      return FALSE;

    case UPLOAD_ERR_PARTIAL:
    case UPLOAD_ERR_NO_FILE:
      \Drupal::messenger()->addError(t('The file %file could not be saved because the upload did not complete.', ['%file' => $file_info->getFilename()]));
      \Drupal::messenger()->addError(t('The file %file could not be saved because the upload did not complete.', ['%file' => $original_file_name]));
      return FALSE;

    case UPLOAD_ERR_OK:
@@ -984,7 +985,7 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va

    default:
      // Unknown error
      \Drupal::messenger()->addError(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $file_info->getFilename()]));
      \Drupal::messenger()->addError(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $original_file_name]));
      return FALSE;

  }
@@ -992,7 +993,7 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va
  $values = [
    'uid' => $user->id(),
    'status' => 0,
    'filename' => trim($file_info->getClientOriginalName(), '.'),
    'filename' => $original_file_name,
    'uri' => $file_info->getRealPath(),
    'filesize' => $file_info->getSize(),
  ];
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ public function testFileSaveUploadSingleErrorFormSize() {
    $file_name = $this->randomMachineName();
    $file_info = $this->createMock(UploadedFile::class);
    $file_info->expects($this->once())->method('getError')->willReturn(UPLOAD_ERR_FORM_SIZE);
    $file_info->expects($this->once())->method('getFileName')->willReturn($file_name);
    $file_info->expects($this->once())->method('getClientOriginalName')->willReturn($file_name);
    $this->assertFalse(\_file_save_upload_single($file_info, 'name'));
    $expected_message = new TranslatableMarkup('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_name, '%maxsize' => format_size(Environment::getUploadMaxSize())]);
    $this->assertEquals($expected_message, \Drupal::messenger()->all()['error'][0]);