phpdrupaldrupal-7drupal-webformhook-form-alter

Using Hook_form_alter on webform submitted values


Drupal 7. Webforms 3.x.

I am trying to modify a webform component value on submit. I made a custom module called 'mos' and added this code to it.

function mos_form_alter(&$form, $form_state, $form_id) { 
  if ($form_id == 'webform_client_form_43') {
      dsm($form['#node']->{'webform'}['components']['1']);
      $form['#submit'][] = 'mos_contact_us_submit';
    }
}
function mos_contact_us_submit($form, &$form_state) {
  $form['#node']->{'webform'}['components']['1'] = 'working@mos.com';
}

However when I look at the results in the database the regular, non-overridden value is stored. Can you help let me know what I am doing wrong?

Eventually I want to take the input value and output an email address based on what was provided (for example. 24 turns into bob@somewhere.com) But I think I can figure this part out myself.


Solution

  • You should to place your submit first.

    array_unshift(
          $form['actions']['submit']['#submit'], 
          'mos_contact_us_submit'
    );
    

    However, if you want to change some variables in form_state, you should to using custom _valadate function.