Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Created May 31, 2013 16:16
Show Gist options
  • Save DuaelFr/5686084 to your computer and use it in GitHub Desktop.
Save DuaelFr/5686084 to your computer and use it in GitHub Desktop.
Get webform submission data keyed by component's machine names instead of component's ids
<?php
/**
* Implements hook_mail_alter().
*
* Add the wanted attachment to submission email.
*
* @param Array $message
*/
function MODULE_mail_alter(&$message) {
if ($message['id'] == 'webform_submission' && $message['params']['node']->uuid == 'UUID OF YOUR WEBFORM') {
$data = _tools_webform_get_submission_data($message['params']['submission'], $message['params']['node']);
// Use $data here
}
}
/**
* Get webform components keys - cids relation
*
* @param stdClass $node
* The webform node
*
* @return Array
* form_key => cid
*/
function _tools_webform_get_components($node) {
$cmps = array();
if (!empty($node->webform)) {
foreach ($node->webform['components'] as $cmp) {
$cmps[$cmp['form_key']] = $cmp['cid'];
}
}
return $cmps;
}
/**
* Get an exploitable array of submission data indexed by components' form_key
*
* @param stdClass $submission
* The webform submission
* @param stdClass $node
* The webform node [optionnal]
*
* @return Array
* form_key => mixed data
*/
function _tools_webform_get_submission_data($submission, $node = NULL) {
if (NULL == $node) {
$node = node_load($submission->nid);
}
$cmps = _tools_webform_get_components($node);
$data = array();
foreach ($cmps as $key => $cid) {
if (!empty($submission->data[$cid])) {
$data[$key] = $submission->data[$cid]['value'][0];
} else {
$data[$key] = '';
}
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment