From 6c93f38f642648cc0576abae5abe2ae26d58ec20 Mon Sep 17 00:00:00 2001 From: catch Date: Fri, 28 Oct 2022 10:20:08 +0100 Subject: [PATCH] Issue #2867336 by mbovan, jcnventura, Berdir, hchonov, gabesullice, jonathanshaw: File size validator should only respect the explicitly configured maximum file size (cherry picked from commit c647ef4daf8866c1aba92436f251127a7aaaf00c) --- .../file/src/Plugin/Field/FieldType/FileItem.php | 2 +- .../Constraint/FileValidationConstraintValidator.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index dc37bf03502..c8c96c25420 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -180,7 +180,7 @@ class FileItem extends EntityReferenceItem { '#type' => 'textfield', '#title' => $this->t('Maximum upload size'), '#default_value' => $settings['max_filesize'], - '#description' => $this->t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit %limit).', ['%limit' => format_size(Environment::getUploadMaxSize())]), + '#description' => $this->t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes could be limited only by PHP\'s maximum post and file upload sizes (current limit %limit).', ['%limit' => format_size(Environment::getUploadMaxSize())]), '#size' => 10, '#element_validate' => [[static::class, 'validateMaxFilesize']], '#weight' => 5, diff --git a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php index a6da920c669..cc7a17def8c 100644 --- a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php +++ b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php @@ -2,6 +2,7 @@ namespace Drupal\file\Plugin\Validation\Constraint; +use Drupal\Component\Utility\Bytes; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -23,6 +24,17 @@ class FileValidationConstraintValidator extends ConstraintValidator { $file = $target->getValue(); // Get the validators. $validators = $value->getUploadValidators(); + + // Always respect the configured maximum file size. + $field_settings = $value->getFieldDefinition()->getSettings(); + if (array_key_exists('max_filesize', $field_settings)) { + $validators['file_validate_size'] = [Bytes::toNumber($field_settings['max_filesize'])]; + } + else { + // Do not validate the file size if it is not set explicitly. + unset($validators['file_validate_size']); + } + // Checks that a file meets the criteria specified by the validators. if ($errors = file_validate($file, $validators)) { foreach ($errors as $error) {