[Drupal] How to process webform submitted data?

| | 1 min read

While dealing with webforms we may need to integrate webform fields to external API or may need to save data to a custom table. In case of webform we don't need to add custom submit handler to deal with submitted data.

You may also look into other solved issues and details related to Drupal webform over here.

Webform hooks can be used to manage this. We can use 'hook_webform_submission_insert($node, $submission)' for the above mentioned context.

There is a little difference in the array structure of webform from normal drupal forms, which may leads to certain confusions in processing submitted data.

For webform field name is saved in the array $node and submissions values are saved $submissions mapped with array keys. We can easily get the 'field name' as 'keys' and 'form submission values' as 'array values' by doing the following.

    $components = $node->webform['components'];
    $data = $submission->data;
    $labels = array();
    foreach ($components as $key => $component) {
      $labels[$key] = $component['form_key'];
    }
    $fields = array();
    foreach ($data as $key => $values) {
      $fields[$labels[$key]] = $values['value'][0];
    }

Hope this saved your time.

You can find more help with a wide range of Drupal services that we offer. Contact us to know more.