So I'm using Pluploader in a custom Form. I've noticed that when files are attached and then the form is submitted (with form errors), when it refreshes the page, the files are no longer there.

To fix this, I added some custom code that fetched the form_state values, passed them to a custom js file via Drupal.settings js, and then added them as already-completed to the pluploader object. Here is the code I used (you'll have to use your own form ids and whatnot, of course):

In the drupal form creation function:

	// If there were any previously-uploaded files, re-attach them via js here.
	$count = 0;
	$files = array();

	while( isset($form_state['input']['edit-step4-data_'.$count.'_tmpname']) ) {
		$id = explode('.', $form_state['input']['edit-step4-data_'.$count.'_tmpname']);

		// Get the file size.
		$size = filesize( drupal_realpath('temporary://'.$id[0].'.tmp') );

		$files[] = array(
			'id' => $id[0],
			'name' => $form_state['input']['edit-step4-data_'.$count.'_name'],
			'size' => $size,
		);

		$count++;
	}

	if ( !isset($form['#attached']) ) { $form['#attached'] = array(); }
	if ( !isset($form['#attached']['js']) ) { $form['#attached']['js'] = array(); }

	$form['#attached']['js'][] = array(
		'data' => array(
			'bcrsp_cmp_files' => $files,
		),
		'type' => 'setting',
	);

	$form['#attached']['js'][] = drupal_get_path('module', 'bcrsp_cmp') . '/js/bcrsp_cmp_new_activity_page.js';

and the javascript file:

(function ($) {
	Drupal.behaviors.bcrsp_cmp_new_activity_page = {
		attach: function (context, settings) {
			// Get the Plupload object.
			var uploader = jQuery(".plupload-element", context).pluploadQueue();

			// Add previous files to uploader (flag as uploaded, as they already were).
			for ( var i = 0; i < Drupal.settings.bcrsp_cmp_files.length; i++ ) {
				var file = Drupal.settings.bcrsp_cmp_files[i];
				var new_file = new plupload.File (file.id, file.name, file.size);
			
				// Add extra data to the file to say it's already uploaded (which it is, since it was passed in from form_state).
				new_file.status = plupload.DONE;
				new_file.loaded = new_file.size;
				new_file.percent = 100;

				// Push the file into the uploader's file list.
				uploader.files.push( new_file );

				// Trigger that an upload happened so that form_state gets properly filled.
				uploader.trigger("UploadFile", new_file );
			}

			// Update the uploader to reflect the new files.
			if ( uploader.files.length > 0 ) {
				var files_loaded = 0;

				// Add up the total bytes that were uploaded.
				for( var i = 0; i < uploader.files.length; i++) {
					files_loaded += uploader.files[i].loaded;
				}

				// Update the queue progress to reflect the files that are updated.
				var queueprogress = uploader.total;
				queueprogress.size = files_loaded;
				queueprogress.loaded = files_loaded;
				queueprogress.uploaded = uploader.files.length;
				queueprogress.percent = 100;

				// Refresh the uploader so it shows the new files and percentages.
				uploader.trigger("QueueChanged");
				uploader.refresh();
			}
		}
	};
})(jQuery);

If I just missed something in my setup to make this work out-of-the-box, let me know. Otherwise, I'll add it to the plupload project and put up the patch here.

Comments

atk@ya.ru’s picture

This problem appeared after upgrade to drupal 7.23 (after 7.22)

HerVil’s picture

The same here : downgrade to drupal 7.22 restore the situation.
With 7.23, i got no error but files disappeared after load.
I'm using Plupload on generic file_field.

nagy.balint’s picture

Check what happens after you apply this patch described here:
#2063161: Multi Upload with plupload broken when using Drupal 7.23
The patch has to be applied to the "Plupload integration" module.

PaulDinelle’s picture

That patch didn't fix my form issues, unfortunately.

slashrsm’s picture

@PaulDanielle: Please confirm that I understand this issue correctly. You've added some files to plupload, but NOT uploaded them yet. Then you submit your form and expect files to stay in Plupload element, still waiting for being uploaded.

alforddm’s picture

I'm having a similar problem...After the files are uploaded. If there is a form error, including but not limited to, mollum flag (which adds a captcha to the form). The files that have been uploaded are lost.

eliosh’s picture

I can confirm PaulDinelle solution works like a charm for a custom form.

Drupal 7.26
Plupload 1.6

le72’s picture

Same here. I am using plupload element in custom form. If form has error, all fields are saved but not plupload :-(

slashrsm’s picture

Somebody please confirm #5 describes what you're trying to achieve.

le72’s picture

slashrsm, no #5 is not my case. I have added some files to plupload, and uploaded them via "upload" button (but DO NOT submit form). Then I submit form and get error for one of the fields (not plupload). The all fields defaulted to values I have input before submitting the form, but plupload NOT.

slashrsm’s picture

Status: Active » Closed (works as designed)

And that is how Plupload works. Once files are uploaded it is up to form implementation to decide what to do with them. By design, you cannot "put" files in Plupload element when building form.