How to limit number of files uploaded with plupload integrtion module? 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' =&gt; array(
  'cardinality' =&gt; 1,


does not work, and I wasn't able to find any other parameter for this.
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 &gt;= 1) {
          var i = 0;
          for (i=0;i&lt;local_uploader_element.files.length;i++) {
            if (i &gt;= 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.