Hi,
I've tried to search in the APIs but I didn't find anything helpful. I've installed the upload_element module, it works fine and uploads the images. I've altered a form (it's a Ubercart 'add to cart' one) in order to have it displayed as a new field into the form, but now I'd like to hook the form submit in order to associate that image file to something else (in this case, with the order being created with the form). How do I catch the image's data on submit? Is there a hook that lets me process any data after the submit function?

Thanks,
m.

Comments

fininho’s picture

..any hint on the submit hook?

rschwab’s picture

Forms in drupal can have multiple submit functions. Because you probably want to let the original submit function do its thing as well, you will want to add your function to the $form['#submit'] array rather than just replace it. Here is an example:

<?php

function yourmodule_YourFormID_form_alter(&$form, &$form_state) {

      $form['#submit'][] = 'yourmodule_submit_function';
}
?>

Then in your new submit function you can do whatever you like. Make sure you do not include the ?> in your code, it breaks drupal.

- Ryan

fininho’s picture

Thanks, it's a useful hint. I've tried to use it, I've altered the original form adding an image upload field, and every new #submit is added to the original form submit chain. I want to catch the moment in which the image file is uploaded, and I don't see any embedded form ito the original one with its own form_id, so I can't modify the image's form. Even hook_insert is not useful, I just can see the image uploaded in 'files' table and I can't catch its parameters.
The original form containing the image is updated with the image's thumbnail, but it does not have any other parameter linked to the image uploaded.
So, how can I link that image to the other fields in the form?

Thanks,
m.

rschwab’s picture

You can catch all the info for your file by declaring a variable in your submit function, something like this:

<?php
$file = $form_state['clicked_button']['#post']['files']['yourfileid'];
$path = $file->filepath;
?>

- Ryan

fininho’s picture

Thanks again, Ryan. I didn't understand at first, but it has been a help. Following the solution to my problem for anyone needing it.

The code to add the image field and upload to the original form:

/**
 * Implementation of hook_form_alter().
 */
function mmm_form_alter($form, $form_state, $form_id) {
  if (isset($form['#id']) && (strpos($form_id,'uc_product_add_to_cart_form') === 0)) {
	## To add the element to the form
	  $form['image'] = array(
		'#type' => 'image_upload_element',
		'#title' => t('Image'),
		'#weight' => '0',
		'#default_value' => $node->image, // {files} object
	  );
        // this adds a submit to the chain
	$form['#submit'][] = 'order_link_to_added_params';
	}
}

The function to manage data, here it just sets a message with some useful params but you should use this to store data:

function order_link_to_added_params($form, &$form_state){
	$img = $form_state['values']['image'];
	$output = 'Submit caught. Data:<br>';
	$output .= 'user session id: '.session_id().
	', user id: '.$img->uid.', product node id: '.$form_state['values']['nid'].'<br>'.
	'<b>file id:</b> '.$img->fid.', filename: '.$img->filename.', stored in: '.$img->destination.
	', <b>cart id:</b>'.$_SESSION['uc_cart_id'];
	drupal_set_message($output);
}

And this is valid for every form using upload_image, apart from uc_cart_id param. Now some more info, as I'm using this to add an image to an order.
Since I'm managing an order with Ubercart, here you can find other data caught while adding to cart. Again, just setting a message here:

/**
 * Implementation of hook_add_to_cart().
 */
function mmm_add_to_cart($nid, $qty, $data) {
  drupal_set_message('add_to_cart. Session id: '.session_id().', <b>cart id:</b>'.$_SESSION['uc_cart_id'].', product id: '.$nid);
  return $result;
}

The rest of the code will be the insertion of uc_cart_id, fid and order_id in a custom table, to have the image(s) linked to the order.

Hope it helps,
m.

alansch’s picture

Just thought I'd weigh in on this as it has helped me considerably... (thanks, Ryan)

The submit functions are executed in order with the original function at the head of the array.
Ryan's code above will add your new submit function to the end of the array.
If you need to pre-process some of the form data before the original submit function sees it, the array_unshift function will prepend an element to the submit function array, allowing your added submit function to be first in the execution order.

function yourmodule_YourFormID_form_alter(&$form, &$form_state) {
  array_unshift($form['#submit'], 'yourmodule_submit_function');
}

function yourmodule_submit_function($form, &$form_state) {
/* any custom submit processing 
 * required for the submitted form data
*/
}