2

I'm using Plupload integration module. In my Form API form I want to use it as simple replacement for file element. However, both file and managed_file types accept one file. plupload accepts any number of files. How to limit it to only defined number of files? Or simply one?

Setting cardinality as FileField Sources Plupload does:

'#plupload_settings' => array(
  'cardinality' => 1,

does not work, and I wasn't able to find any other parameter for this.

1 Answer 1

0

The way I limited it to single file, was to create js file and attach it to the form. In that file, I bound a function to QueueChanged, to remove excess files and hide "Add file" button:

(function($) {

Drupal.plupload = Drupal.plupload || {};

/**
 * Attaches the Plupload behavior to each Plupload form element.
 */
Drupal.behaviors.download_plupload = {
  attach: function (context, settings) {
    $(".plupload-element", context).once('download-plupload-init', function () {
      var add_button = $(this).find('.plupload_button.plupload_add');
      var uploader = $(this).pluploadQueue();
      uploader.bind('QueueChanged', function() {
        var local_uploader_element = this;
        // Remove files exceeding the cardinality setting.
        if (local_uploader_element.files.length >= 1) {
          var i = 0;
          for (i=0;i<local_uploader_element.files.length;i++) {
            if (i >= 1) {
              $(this).removeFile(uploader.files[i]);
            }
          }
          add_button.hide();
        }
        else {
          add_button.show();
        }
      });
    });
  }
};

})(jQuery);

This is a simplified version of js found in FileField Sources Plupload, trimmed, without Field API specific parts, and made to work on all plupload elements.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.