Skip to content

Instantly share code, notes, and snippets.

@bfodeke
Last active January 18, 2017 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfodeke/e3a33feab8fccbdef5621a850580761f to your computer and use it in GitHub Desktop.
Save bfodeke/e3a33feab8fccbdef5621a850580761f to your computer and use it in GitHub Desktop.
plupload implementation within Drupal with validators.
/**
* PLUPLOAD example form to showcase plupload.
*/
function MYMODULE_some_cool_form() {
$form['file_upload'] = array(
'#type' => 'plupload',
'#title' => t('Upload Files'),
'#required' => TRUE,
'#description' => t('Allowed file extensions: doc docx pdf tif tiff xls xlsx'),
'#upload_validators' => array(
'file_validate_extensions' => array('doc docx pdf tif tiff xls xlsx'),
'mymodule_validate_file_extensions' => array('doc docx pdf tif tiff xls xlsx'),
),
'#plupload_settings' => array(
'runtimes' => 'html5',
'drop_element' => FALSE,
'chunk_size' => '1mb',
'multi_selection' => TRUE,
),
'#event_callbacks' => array(
'FilesAdded' => 'Drupal.myModule.FilesAdded',
'UploadComplete' => 'Drupal.behaviors.myModule.UploadComplete',
'FilesRemoved' => 'Drupal.behaviors.myModule.FilesRemoved',
),
);
}
/**
* Custom file validator.
*/
function mymodule_validate_file_extensions($file, $allowed_extensions) {
$error = file_validate_extensions($file, $allowed_types);
return $error;
}
/**
* Submit handler for MYMODULE_some_cool_form().
*/
function MYMODULE_some_cool_form_submit($form, &$form_state) {
$uploaded_files = ['file_upload'];
$scheme = 'private://';
foreach ($uploaded_files as $uploaded_file) {
if ($uploaded_file['status'] == 'done') {
$source = $uploaded_file['tmppath'];
$destination = file_stream_wrapper_uri_normalize($scheme . $uploaded_file['name']);
$destination = file_unmanaged_move($source, $destination, FILE_EXISTS_REPLACE);
$file = plupload_file_uri_to_object($destination);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}
}
-------- In Javascript ----------
/**
* Restricting uploaded files to X number of files (maxFiles).
*/
!(function ($) {
'use strict';
Drupal.behaviors.myModule = {
FilesAdded: function(up, addedFiles) {
var addButton = $('#' + up.settings.browse_button);
var numFiles = up.files.length;
var maxFiles = 1;
// We can only upload 1 file at a time to each order.
if (numFiles > maxFiles) {
alert('No more than ' + maxFiles + ' file(s) can be uploaded');
// Remove the newly added file(s).
up.splice(maxFiles);
}
if (numFiles === maxFiles) {
// Disable uploading and also hide the button.
up.trigger("DisableBrowse", true);
addButton.hide();
}
},
FilesRemoved: function(up, removedFiles) {
var numFiles = up.files.length;
var addButton = $('#' + up.settings.browse_button);
var maxFiles = 1;
if (numFiles < maxFiles) {
up.trigger("DisableBrowse", false);
addButton.show();
}
}
// Other events documented here: http://www.plupload.com/example_events.php#source
}
})(jQuery);
@bfodeke
Copy link
Author

bfodeke commented Jan 4, 2017

Cannot figure out how to make plupload respect the file extension list

@bfodeke
Copy link
Author

bfodeke commented Jan 4, 2017

Solved with custom file validator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment