3

I am beginning a module which implements the hook_webform_submission_insert. I am successfully getting the following data from my implementation of that hook:

stdClass Object
(
    [nid] => 3401
    [uid] => 1
    [submitted] => 1425491295
    [remote_addr] => http://myserver.com
    [is_draft] => 0
    [data] => Array
        (
            [2] => Array
                (
                    [value] => Array
                        (
                            [0] => Chandler Mann
                        )

                )

            [3] => Array
                (
                    [value] => Array
                        (
                            [0] => tecut[email protected]
                        )

                )
.
.
.

I need to write code to programmatically map each value index to node fields in a process of creating nodes from custom code.

However, as the output above does not have field names, it's difficult to map correctly to the necessary node fields.

Can anyone advice how I might discover what name webform assigns to the field#id 2, 3 etc as in my code sample above?

0

2 Answers 2

2

The $node argument to hook_webform_submission_insert() contains the info you're looking for. For a Webform node, you'll find an array $node->webform->components where, for example, $node->webform->components[2] would correspond to the item whose value is "Chandler Mann" in your data.

2

The $submission->data array is keyed by Webform internal component IDs, which you can't see from the admin interface.

You can build a helper array mapping form keys to component IDs:

$compMap = array();
foreach ($node->webform['components'] as $c) {
  $compMap[$c['form_key']] = $c['cid'];
}

Then you can get the values by form keys as follows:

if (array_key_exists($compMap[$key], $submission->data)) {
  $value = $submission->data[$compMap[$key]]['value'][0];
}
else {
  // No value was entered for this field.
}

Note, that the value will be in the ['value'][0] element only for "simple" fields, like text or number. For other types, e.g. reference fields, check your data.

Your Answer

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

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