Hi,
I've altered a product form using hook_form_alter, adding two fieldsets in which the first shows some text and hidden input and the second a file upload field, implemented with managed_file.
I'm using hook_uc_add_to_cart_data to manage input from the form. When I only have hidden and text fields everything goes fine (even if the hook is called twice), but when I upload a file the other fields become empty: $form_values does not contain any more custom data, like if it was sent with the ajax file submit.
'Solved' using type file instead of managed_file, for now.

Comments

longwave’s picture

Category: bug » support
Status: Active » Postponed (maintainer needs more info)

Not sure why this is a bug report, nor what we can do about it in Ubercart? If there is a problem with managed_file it should perhaps be reported against Drupal core? Your custom code might be helpful too, but in general we don't support custom code in this issue queue.

fininho’s picture

Thanks for your answer.
It seems a bug to me as I loose form data, and I don't think it should happen. I don't know if this is a Drupal issue due to the way form data are handled, or a Ubercart issue due to the values passed to hook_add_to_cart_data, so I posted it here under Ubercart project.

longwave’s picture

If you post your code someone may take a look at it, perhaps even it would be useful in order to get a 'file' product attribute into Ubercart eventually.

fininho’s picture

Here's the code, hoping someone finds it useful.

<?
/** hook_form_alter
*/
function blizzart_form_alter(&$form, &$form_state, &$form_id) {
  if (isset($form['#id']) && (strpos($form_id,'uc_product_add_to_cart_form') === 0)) {
    // ...
	$form['upload-img'] = array(
	  '#type' => 'fieldset',
	  '#title' => t('Upload Image'),
	  '#weight' => -3,
	  '#collapsible' => TRUE,
	  '#collapsed' => TRUE,
	); 
	## Adds the image upload element to the form
	$form['upload-img']['image'] = array(
//
// TO HIT ERROR REPLACE NEXT LINE WITH: '#type' => 'managed_file',
//
		'#type' => 'file', // managed_file is buggy and clears the other form fields.
		'#title' => t('Custom Label'),
		'#weight' => '0',
	);
	
	// ... 

	$form['template-img'] = array(
	  '#type' => 'fieldset',
	  '#title' => t('Choose from Template'),
	  '#weight' => -20,
	  '#collapsible' => TRUE,
	  '#collapsed' => FALSE,
	);
	$form['template-img']['galleria'] = array(
		'#markup' => '<div class="jMyCarousel">'.$img_gallery.'</div>',
		'#weight' => -3,
	);
	$form['template-img']['img-scelta'] = array(
		'#markup' => '<div id="img-scelta-'.$gallery_path.'" class="bcViewDiv bcViewDiv'.$gallery_path.'" ><img class="bcView bcView'.
			$gallery_path.'" src="'.base_path().drupal_get_path('module', 'blizzart').'/'.$gallery_path.'/gallery'.
			'/default.jpg"></div>',
		'#weight' => -1,
	);
	
	// hidden field
	// --> this looses its value using managed_file
	$form['template-img']['gallery_path'] = array(
		'#type' => 'hidden',
		'#id' => 'gallery_path',
		'#value' => $gallery_path,
		'#weight' => -2,
	);
}

/** Ubercart hook
*/
function blizzart_uc_add_to_cart_data($form_values){
	// ..
	
	// custom function, see below
	$newvars = link_order_to_extradata($form_values);
	return $newvars;
}

/** Saves into the DB additional info. It calls another custom function based on the chosen gallery name
*/
function link_order_to_extradata($form_values){
	// ...

	// here hits an error. $form_values['gallery_path'] is found for no file uploaded into $form['upload-img']['image'],
	// while it gives an index array error if a file has been uploaded. 
	if (!is_null($form_values['gallery_path'])){ 
		// ...
	}
}
longwave’s picture

Status: Postponed (maintainer needs more info) » Active

I guess this may need special handling in the same way that system_settings_form() does not handle managed_file properly either: #1090198: managed_file form item in system settings form causes error in form_state_values_clean()

Leaving active for now as I think we have enough info, but someone needs to take the time to debug this further.

longwave’s picture

Status: Active » Closed (duplicate)

Pretty sure this is a core issue related to managed_file inside fieldsets, #1957602: managed_file inside fieldset returns 0 upon submission without previous upload sounds very similar as well. Please reopen only if you are sure this is an actual problem with Ubercart.