简体   繁体   English

在Webform提交的值上使用Hook_form_alter

[英]Using Hook_form_alter on webform submitted values

Drupal 7. Webforms 3.x. Drupal 7. Webforms3.x。

I am trying to modify a webform component value on submit. 我正在尝试在提交时修改Webform组件的值。 I made a custom module called 'mos' and added this code to it. 我制作了一个名为“ mos”的自定义模块,并将此代码添加到其中。

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. 最终,我想根据输入的内容来输入值并输出一个电子邮件地址(例如,24变成bob@somewhere.com),但是我想我可以自己弄清楚这部分。

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. 但是,如果要更改form_state中的某些变量,则应使用自定义_valadate函数。

I got it! 我知道了! BIG Thanks to @dobeerman for pointing me in the right direction. BIG感谢@dobeerman向我指出正确的方向。 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. 这样,我可以将电子邮件地址设为私有,但仍使用_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! 我不小心弄乱了我的帐户创建过程,所以我不能给你信用dobeerman,但我通过电子邮件发送给管理员,希望我能弄清楚它,以便给您带来一些帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM