For my sign-up application, I'd be very happy if I can cause the uid of a submission to change. This would happen if a form is submitted by another party or an anonymous party, but is validated to be a user. (Sample snippet available to those interested)

I'd like to change the uid from "0" to the uid of the person identified. Can that be done in the advanced features, or must I do some external code?

Thanks!

Comments

cayenne’s picture

Title: Post-submission change of uid? » Post-submission change of submitter's uid?

Maybe a little clarity: I am not trying to change the user's uid, I am trying to associate the submission with the uid of a user identified elsewhere, either during validation, which I am doing now, or wherever I do the reassignment. Worse comes to worse, I can run a mySQL query from time to time, but I's like to do it at submit time.

Any clues?

cdale’s picture

EDIT: Sorry, below code is for D6. I'm not as familiar with D5, but a similar process should work.

I don't think this is possible to do using any feature offered by webform without modifying the webform code. You should be able to do it with a small amount of custom code however.

Something like the following *should* work, although I have not tested it. You would need to do this in a module.


/**
 * Implementation of hook_form_alter().
 */
function custom_form_alter(&$form, &$form_state, $form_id) {
  // Only affect webform client forms.
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['#submit'][] = 'custom_webform_client_form_submit';
  }
}

/**
 * Submit callback for webform_client_form.
 */
function custom_webform_client_form_submit($form, &$form_state) {
  // Only change new submissions.
  if ($form_state['values']['details']['is_new']) {
    $sid = $form_state['values']['details']['sid'];
    $uid = 3; // You can use submitted form values in $form_state['values']['submitted'] to determine the uid if you don't know what it is.
    db_query('UPDATE {webform_submissions} SET uid = %d WHERE sid = %d', array($uid, $sid));
  }
}
cdale’s picture

Here is some code that should work for D5.


/**
* Implementation of hook_form_alter().
*/
function custom_form_alter($form_id, &$form) {
  // Only affect webform client forms.
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['#submit']['custom_webform_client_form_submit'] = array();
  }
}

/**
* Submit callback for webform_client_form.
*/
function custom_webform_client_form_submit($form_id, &$form_values) {
  // Only change new submissions.
  if ($form_values['details']['is_new']) {
    $sid = $form_values['details']['sid'];
    $uid = 3; // You can use submitted form values in $form_values['submitted'] to determine the uid if you don't know what it is.
    db_query('UPDATE {webform_submissions} SET uid = %d WHERE sid = %d', array($uid, $sid));
  }
}

quicksketch’s picture

Just to backup what cdale has said, there's no way to do this in Webform without custom coding. The approach he's used above is about as close as you can get.

cayenne’s picture

Thank you both very much. I am not surprised that this does not work within Webform. The fix looks very implementable! I'll give it a try later this week and report back.

passa’s picture

hi,

I join this post having a similar pb.
I'd like to be able to alter a webform.

i try both solution:

function webform_client_form_alter(&$form, $form_state, $form_id) {
	print_r("tptptptptptptptptpt");
	  watchdog("doctorForm", "We load a client form");
      $form['passa']['#type'] = "hidden";
      $form['passa']['#value'] = "ZZZZZZZZ";

     //$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
}

function custom_form_alter(&$form, &$form_state, $form_id) {
  // Only affect webform client forms.
  watchdog("doctorForm", "We load a custom form");
  if (strpos($form_id, 'webform_client_form_') === 0) {
  		print_r("tptptptptptptptptpt");
	  watchdog("doctorForm", "We load a client form");
    	$form['#submit'][] = 'custom_webform_client_form_submit';
  }
}

Loading a webform is not calling any of those function ...
No "tptptpt" print and nothing is log ....
Any tips ?

thanks.

avior’s picture

Hi
the code above had drupal 5 signature
so here is drupal 6 working code

function general_form_alter(&$form, $form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['#submit'][] = 'general_webform_submit';
    
  }
}

function general_webform_submit($form, &$form_state) {
  if ($form['form_id']['#value') == 'webform_client_form_26'){
    drupal_set_message("Do extra stuff to submitted form here");
    dsm($form);
    dsm($form_state);
    
    
  }
  
  
}


quicksketch’s picture

Status: Active » Closed (fixed)

Looks like this got wrapped up. Support for writing custom code is no longer provided in the Webform queue, please do not reopen.