Using Hook_form_alter on webform submitted values

泪湿孤枕 提交于 2019-12-13 03:56:52

问题


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.


回答1:


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.




回答2:


I got it! BIG Thanks to @dobeerman for pointing me in the right direction. Here is the code that ended up working:

function mos_form_alter(&$form, &$form_state, $form_id) {
  if ('webform_client_form_43' == $form_id) {
    //dsm($form);
    $form['#validate'][] = 'mos_check_email';   
  }
}

function mos_check_email(&$form, &$form_state, $form_id) {
    $emailVal = $form_state['values']['submitted']['to'];
    switch($emailVal) {
        case 1: $emailVal = 'email@test.com'; break;
        case 2: $emailVal = 'email2@test.com'; break;
        case 3: $emailVal = 'email3@test.com'; break;
                ......
    }
    $form_state['values']['submitted']['to']=$emailVal;
    //dpm($form_state);
}

This way I can keep email address private, but still pass variables to the form with _GET. Kind of a weird situation... but we are trying to keep some existing code intact, so it seemed like the best route.

I accidentally messed up my account creation, so I can't give you the credit dobeerman but I emailed the admins and hopefully I will get it straightened out to get you some rep!



来源:https://stackoverflow.com/questions/5560133/using-hook-form-alter-on-webform-submitted-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!