More useful data when editing Drupal (7) webform templates
I really like Drupal's Webform module, especially when managing one-way communication or information that has limited utility beyond the initial submission.
One powerful use of Webform is to e-mail results to a set (or dynamic) list of recipients. The stock install has a serviceable rendering of the submitted elements, though if you want to do any advanced manipulation of the data before spitting it out, you're hard-pressed to find a hierarchical tree merging the webform structure with the submitted values.
If you know the component ID of a particular field you're working with, then great. But if you're dealing with a big form with many dozens of components, searching for the proper component in every individual case would be too cumbersome.
Webform's webform-mail.tpl.php template provides you a $node object representing the form structure, and $submission which contains a flat set of submitted values. You need information from both to make an array with depth. You can use a built-in function from Webform to get a tree of the data. It helps if you merge it ahead of time with the submitted info.
<?php
$merged = $node->webform['components'];
//Fields - map values from submission object
foreach($merged as &$c) {
if(isset($submission->data[$c['cid']]['value'][0]))
$c['value'] = $submission->data[$c['cid']]['value'][0];
}
$pg = 1;
$tree = array();
_webform_components_tree_build($merged, $tree, 0, $pg);
?>In short, we need the values from $submission, but the field name and (especially) parent fieldset from $node.
You can further extend this to create arrays for particular fieldsets, for instance to create an array of values based on field names, or even their values. There's likely a different approach at form submission using webform_php, but in this case I wanted to operate inside Webform's existing e-mail process, not independent of it.
I'm guessing this code is pretty independent of Drupal core, but in full disclosure, I wrote this for D7 and your mileage may vary in previous versions.
If you know of a way to get Webform to provide us a merged array from the get-go, let me know.
