3

I want to create an associative array of submitted form data and do some additional processing on the submitted data (namely send the data over http POST). I have the following code:

function webformssalesforce_form_alter(&$form, &$form_state, $form_id) {
    if (substr($form_id, 0, 20) == 'webform_client_form_') {
        // Add the submit handler after the existing Webform submit handler,
        // but before the second Webform handler. Pop off the first one and add
        // ours second.
        $first = array_shift($form['#submit']);
        array_unshift($form['#submit'], $first, 'webformssalesforce_client_submit');
      }


}

function webformssalesforce_client_submit($form, &$form_state) {
    $node = node_load($form_state['values']['details']['nid']);

    $fields =  Array();
    foreach($form_state["values"]["submitted_tree"] as $key=>$value) { 
        if(! is_array ( $value)){
            $fields[$key] = $value; 
        }
    }

    //do stuff with $fields


}

This works fine for text and email fields, however for select elements I get the option's safe_value instead of the display value for the select. So for example if an element has the folowing options:

1|Display value 1
2|Display value 2
3|Display value 3

How can I get the display value instead of the safe_value (Display value 2 instead of 2) for an element?

I'm pretty sure this code worked fine for me with a previous version of webform.

2

I've run into the same issue, could not find a predefined function and ended up writing one that loops over the select options in the text area and returns an associative array.

The basic principle is described in this article: http://blog.raisedeyebrow.com/2009/01/displaying-webform-results-in-a-block/

For my project I used the webform_submission_insert hook instead of form_alter, but I hope it works for the variables that you have available in form_alter as well.

If it is likely that you'll need this feature more often you may want to use a function to extract the data for any textarea using the same principle.

function mymodule_webform_submission_insert($node, $submission) {

   switch ($node->title) {

    case "Test form":

    $options = array();

    // fill options field with options from select textarea via custom function;
    // field number of my particular webform select field is 12, hence:
    $options = _extract_select_options ($node->webform['components'][12]['extra']['items']);

    // output key and value of chosen select option
    $chosen = $options[$submission->data[12]['value'][0]]);
    drupal_set_message ("key / value: ".$submission->data[12]['value'][0]." / ".$chosen;

    break;
   }
}

// declare function with text area content as parameter
function _extract_select_options ($mytext) {

    $options = $pairs = $temp_array = array();

    // split text into chunks after each line-break;
    // if not visible here, use "backslash n" as explode parameter
    $pairs = explode("\n", $mytext);

    // split each line into pairs of key and value
    foreach($pairs as $pair) {
      $temp_array = explode('|', $pair);
      $options[trim($temp_array[0])] = $temp_array[1];
    }

    return $options;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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