Commit 46a6535a authored by Primsi's avatar Primsi
Browse files

Issue #2633346 by chr.fritsch, Primsi, phenaproxima: Implement maxFiles

parent e68db0c4
......@@ -71,6 +71,14 @@
uploadedFilesElement.attr('value', newValue);
}
});
// React on maxfilesexceeded. Remove all rejected files.
dropzoneInstance.on("maxfilesexceeded", function () {
var rejectedFiles = dropzoneInstance.getRejectedFiles();
for (var i = 0; i < rejectedFiles.length; i++) {
dropzoneInstance.removeFile(rejectedFiles[i]);
}
});
}
};
......
......@@ -117,6 +117,11 @@ class DropzoneJsEbWidget extends WidgetBase {
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
$cardinality = 0;
$validators = $form_state->get(['entity_browser', 'validators']);
if (!empty($validators['cardinality']['cardinality'])) {
$cardinality = $validators['cardinality']['cardinality'];
}
$config = $this->getConfiguration();
$form['upload'] = [
'#title' => t('File upload'),
......@@ -125,6 +130,7 @@ class DropzoneJsEbWidget extends WidgetBase {
'#dropzone_description' => $config['settings']['dropzone_description'],
'#max_filesize' => $config['settings']['max_filesize'],
'#extensions' => $config['settings']['extensions'],
'#max_files' => ($cardinality > 0) ? $cardinality : 0,
];
$form['#attached']['library'][] = 'dropzonejs/widget';
......
......@@ -28,6 +28,9 @@ use Drupal\Core\Render\Element\FormElement;
* http://www.dropzonejs.com/#config-maxFilesize
* - #extensions
* A string of valid extensions separated by a space.
* - #max_files
* Number of files that can be uploaded.
* If < 1, there is no limit.
*
* When submitted the element returns an array of temporary file locations. It's
* the duty of the environment that implements this element to handle the
......@@ -80,6 +83,12 @@ class DropzoneJs extends FormElement {
$element['#max_filesize'] = file_upload_max_size();
}
// If the element accepts multiple uploads, set #max_files to NULL
// (explicitly unlimited) if #max_files is not specified.
if (empty($element['#max_files'])) {
$element['#max_files'] = NULL;
}
if (!\Drupal::currentUser()->hasPermission('dropzone upload files')) {
$element['#access'] = FALSE;
drupal_set_message("You don't have sufficent permissions to use the DropzoneJS uploader. Contact your system administrator", 'warning');
......@@ -111,6 +120,7 @@ class DropzoneJs extends FormElement {
'maxFilesize' => $max_size,
'dictDefaultMessage' => $element['#dropzone_description'],
'acceptedFiles' => '.' . str_replace(' ', ',.', self::getValidExtensions($element)),
'maxFiles' => $element['#max_files'],
],
],
];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment