Last active
January 18, 2017 20:28
-
-
Save bfodeke/e3a33feab8fccbdef5621a850580761f to your computer and use it in GitHub Desktop.
plupload implementation within Drupal with validators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Solved with custom file validator
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cannot figure out how to make plupload respect the file extension list