1

I am new to drupal but I do have php experience. I am trying to modify an existing custom module that handles some forms on our site. The form in question is used to send an email to the address that is included as a hidden item.

I need to change this email address based on an option the user chooses.

Here is an outline of what I need to happen

user goes to form (hidden email field is already set) user enters their options user clicks submit button check users options and modify email field accordingly save values to database send email to new modified email address

Below is the code I am working with now

function bp_bottled_water_redirect(&$form, &$form_state, $form_id){
$interest = $form_state['input']['submitted']['primary_interest'];
$email = $form['submitted']['dealer_e_mail']['#webform_component']['value'];
//drupal_set_message("email is " . $email);
//drupal_set_message("interest is " . $interest);

if($interest == "bottled_water" && $email == "[email protected]"){
    //$form_state['input']['submitted']['dealer_e_mail'] = "[email protected]";
    //$email = $form_state['input']['submitted']['dealer_e_mail'];
    $form_state['values']['dealer_e_mail'][LANGUAGE_NONE][0]['value'] = "[email protected]";
    drupal_set_message("changed email to: " . $form_state['values']['dealer_e_mail'][LANGUAGE_NONE][0]['value']);

}
else{
    $form['submitted']['dealer_e_mail']['#webform_component']['value'] = $email;
    drupal_set_message("email remains: " . $email);
    drupal_set_message("interest is " . $interest);
    }
}

/**
 * Implements hook_form_alter().
 */
function dealers_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form ID is : " . $form_id);

if ($form_id == 'webform_client_form_960') {
//drupal_set_message("found form");
$form['#submit'][]='bp_bottled_water_redirect';

}

  // Act only on 'webforms'
  if (isset($form['#node']->type) && $form['#node']->type == 'webform') {

    $require_zip = FALSE;
    if (isset($form['#node']->field_form_display_options['und'])){
      foreach ($form['#node']->field_form_display_options['und'] as &$key) {
        if ($key['tid'] == 13) {
          $require_zip = TRUE;
        }
      }
    }
    switch($require_zip){
      case TRUE:
        $zip = _session_get('zip', '');
        if (dealers_is_local($zip) && $require_zip == TRUE && $zip !== '') {
          $zipinfo = json_decode(file_get_contents('http://zip.elevenbasetwo.com/'. $zip));
          $simplexml_obj = dealers_get_local($zip);
          $form['submitted']['5_digit_zip_code']['#default_value'] = $zip;
          // $form['submitted']['city']['#default_value'] = ucwords(strtolower($zipinfo->city));
          $form['submitted']['state']['#default_value'] = $zipinfo->state;
          $form['submitted']['dealer_e_mail']['#default_value'] = $simplexml_obj->DealerList->DealerRecord->EMail;
          $form['submitted']['dealer_e_mail']['#webform_component']['value'] = $simplexml_obj->DealerList->DealerRecord->EMail;
        }
        else {
          drupal_goto('dealers/'. substr($_SERVER['REQUEST_URI'], 1));
        }
        break;
    }
  }
}

The issue I am having is I can change the values according to the drupal_set_message. But it seems to be taking place after the data is saved. Because the email shown when viewing my submission is still the old one. I think my code may be running to late.

How can I modify the email address before the data is saved?

2
  • Your last function seems incomplete.
    – sanzante
    Commented May 6, 2014 at 17:02
  • Yes I left out some of the code. I have now added the rest of the dealers_form_alter function
    – Jeremy
    Commented May 6, 2014 at 17:09

2 Answers 2

0

You have to use hook_webform_submission_presave hook.

Using that you can Modify a Webform submission, prior to saving it in the database

For more information go through the hook documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.