summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Haug2010-12-08 15:04:20 +0000
committerNathan Haug2010-12-08 15:04:20 +0000
commite381a9b0cbbde4af7ab722c736492b4d8c22ea46 (patch)
treeb8ad4910b51ddba12aeecae74083fe4ef89dee38
parent88f7612d64c4ed8aa077bb8ba4961fa38c1376c3 (diff)
#939102 by bleen and quicksketch: Accept attribute contains extensions instead of mimetypes (preventing Chrome from selecting files).
-rw-r--r--filefield.js50
-rw-r--r--filefield_widget.inc18
2 files changed, 36 insertions, 32 deletions
diff --git a/filefield.js b/filefield.js
index ffe84c8..63098fc 100644
--- a/filefield.js
+++ b/filefield.js
@@ -4,32 +4,7 @@
* Auto-attach standard client side file input validation.
*/
Drupal.behaviors.filefieldValidateAutoAttach = function(context) {
- $("input[type='file'][accept]", context).change( function() {
- // Remove any previous errors.
- $('.file-upload-js-error').remove();
-
- /**
- * Add client side validation for the input[type=file] accept attribute.
- */
- var accept = this.accept.replace(/,\s*/g, '|');
- if (accept.length > 1 && this.value.length > 0) {
- var v = new RegExp('\\.(' + accept + ')$', 'gi');
- if (!v.test(this.value)) {
- var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.",
- { '%filename' : this.value, '%extensions' : accept.replace(/\|/g, ', ') }
- );
- // What do I prepend this to?
- $(this).before('<div class="messages error file-upload-js-error">' + error + '</div>');
- this.value = '';
- return false;
- }
- }
-
- /**
- * Add filesize validation where possible.
- */
- /* @todo */
- });
+ $("input[type=file]", context).bind('change', Drupal.filefield.validateExtensions);
};
@@ -72,7 +47,28 @@ Drupal.behaviors.filefieldAdmin = function(context) {
* @param {Object} event
*/
Drupal.filefield = {
- disableFields: function(event){
+ validateExtensions: function(event) {
+ // Remove any previous errors.
+ $('.file-upload-js-error').remove();
+
+ var fieldName = this.name.replace(/^files\[([a-z0-9_]+)_\d+\]$/, '$1');
+ var extensions = '';
+ if (Drupal.settings.filefield && Drupal.settings.filefield[fieldName]) {
+ extensions = Drupal.settings.filefield[fieldName].replace(/,\s*/g, '|');
+ }
+ if (extensions.length > 1 && this.value.length > 0) {
+ var extensionPattern = new RegExp('\\.(' + extensions + ')$', 'gi');
+ if (!extensionPattern.test(this.value)) {
+ var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.",
+ { '%filename' : this.value, '%extensions' : extensions.replace(/\|/g, ', ') }
+ );
+ $(this).before('<div class="messages error file-upload-js-error">' + error + '</div>');
+ this.value = '';
+ return false;
+ }
+ }
+ },
+ disableFields: function(event) {
var clickedButton = this;
// Only disable upload fields for AHAH buttons.
diff --git a/filefield_widget.inc b/filefield_widget.inc
index 994d2fb..139ccc0 100644
--- a/filefield_widget.inc
+++ b/filefield_widget.inc
@@ -263,8 +263,7 @@ function filefield_widget_value($element, $edit = FALSE) {
* remove buttons, and the description field.
*/
function filefield_widget_process($element, $edit, &$form_state, $form) {
- // The widget is being presented, so apply the JavaScript.
- drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');
+ static $settings_added;
$item = $element['#value'];
$field_name = $element['#field_name'];
@@ -273,6 +272,18 @@ function filefield_widget_process($element, $edit, &$form_state, $form) {
$field = $form['#field_info'][$field_name];
+ // The widget is being presented, so apply the JavaScript.
+ drupal_add_js(drupal_get_path('module', 'filefield') .'/filefield.js');
+ if (!isset($settings_added[$field_name]) && isset($element['#upload_validators']['filefield_validate_extensions'])) {
+ $settings_added[$field_name] = TRUE;
+ $settings = array(
+ 'filefield' => array(
+ $field_name => $element['#upload_validators']['filefield_validate_extensions'][0],
+ ),
+ );
+ drupal_add_js($settings, 'setting');
+ }
+
// Title is not necessary for each individual field.
if ($field['multiple'] > 0) {
unset($element['#title']);
@@ -423,9 +434,6 @@ function filefield_widget_process($element, $edit, &$form_state, $form) {
'#type' => 'file',
'#description' => implode('<br />', $desc),
'#size' => 22,
- '#attributes' => array(
- 'accept' => implode(',', array_filter(explode(' ', $field['widget']['file_extensions']))),
- ),
'#access' => empty($item['fid']),
);