diff --git a/plupload_widget.module b/plupload_widget.module new file mode 100644 index 0000000..2796caa --- /dev/null +++ b/plupload_widget.module @@ -0,0 +1,17 @@ + &$data) { + if (isset($data['constraints'])) { + foreach ($data['constraints'] as $constraint_name => $constraint_data) { + if ($constraint_name == 'FileValidation') { + unset($data['constraints'][$constraint_name]); + $data['constraints']['PluploadWidgetWithoutPhpLimitsFileValidation'] = []; + } + } + } + } +} diff --git a/src/Plugin/Field/FieldWidget/FileWidget.php b/src/Plugin/Field/FieldWidget/FileWidget.php index 279ff4d..2985a67 100644 --- a/src/Plugin/Field/FieldWidget/FileWidget.php +++ b/src/Plugin/Field/FieldWidget/FileWidget.php @@ -29,6 +29,20 @@ class FileWidget extends CoreFileWidget { use PluploadWidgetTrait; + /** + * {@inheritdoc} + */ + function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $element = parent::formElement($items, $delta, $element, $form, $form_state); + + // This value is shown in widget to the user. + if (isset($element['#upload_validators']['FileSizeLimit']['fileLimit'])) { + $element['#upload_validators']['FileSizeLimit']['fileLimit'] = $this->getMaxFileSize(); + } + + return $element; + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/Validation/Constraint/WithoutPhpLimitsFileValidationConstraint.php b/src/Plugin/Validation/Constraint/WithoutPhpLimitsFileValidationConstraint.php new file mode 100644 index 0000000..df38a52 --- /dev/null +++ b/src/Plugin/Validation/Constraint/WithoutPhpLimitsFileValidationConstraint.php @@ -0,0 +1,18 @@ +get('entity')->getTarget(); + if (!$target) { + return; + } + + $file = $target->getValue(); + // Get the validators. + $validators = $value->getUploadValidators(); + $max_filesize = Bytes::toNumber($value->getFieldDefinition()->getSetting('max_filesize')); + + if (isset($validators['FileSizeLimit']['fileLimit']) && !empty($max_filesize)) { + $validators['FileSizeLimit']['fileLimit'] = $max_filesize; + } + + /** @var \Drupal\file\Validation\FileValidatorInterface $file_validator */ + $file_validator = \Drupal::service('file.validator'); + + // Checks that a file meets the criteria specified by the validators. + if ($errors = $file_validator->validate($file, $validators)) { + foreach ($errors as $error) { + $this->context->addViolation($error); + } + } + } +}