You can change the rendering of a webform submission easily. See hook_webform_submission_render_alter.
I wrote this code 2 days ago to change a submission embedded in a View, and also change the submission's appearance in a PDF from webform2pdf. The same approach is doable using Emails. I hate webform 4's new tokens. I bypass them completely.
/**
* Implements hook_webform_submission_render_alter().
*
* Add a download PDF link to the beginning of the submission view.
*/
function MYMODULE_webform_submission_render_alter(&$renderable) {
// use Devel to see what $renderable array looks like.
// dpm($renderable);
// If we're on a custom View path embedding a submission, add a download
// link.
if (current_path() == 'open-applications') {
$nid = $renderable['#node']->nid;
$sid = $renderable['#submission']->sid;
$download_pdf_link = l('Download Pdf', "node/$nid/submission/$sid/downloadpdf");
$renderable['reference_regarding']['#prefix'] = $download_pdf_link.'<br>';
} else {
// DEBUG
// ... if we wanted to prefix the $renderable array with say a <style> tag
// we could do that via #prefix. I couldnt get this to work for PDFs.
// $renderable['reference_regarding']['#prefix'] = '**' . substr(current_path(),-11) . '**';
}
// This glorious hack from: https://drupal.org/node/1597892
//
// and see:
// http://drupal.stackexchange.com/questions/50086/how-to-use-submissionvalues-in-email-template-in-webforms
// If we're downloading a PDF, checking the page url, then append break tags
// after a title.
if (substr(current_path(),-11) === 'downloadpdf') {
foreach ($renderable as $i => $item) {
if (is_array($item) && !empty($item['#title'])) {
$renderable[$i]['#title'] .= "<br>";
}
}
}
// Unset a hidden webform field from viewing, which I have pointing to a
// Relation ID.
unset($renderable['webform_rid']);
}
Like I said I bypass the use of tokens in my PDF with the use of a helper function and also custom PHP in the PDF content body (you could do the same for your email body):
/**
* This function builds an array of HTML fragments of rendered submissions from
* a collection of Submission IDs.
*/
function _MYMODULE_render_reference_submissions($sids) {
$content = array();
if (count($sids)) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
$webform_nid = 1;
$webform_node = node_load($webform_nid);
foreach ($sids as $sid) {
$submission = webform_get_submission($webform_nid, $sid, TRUE);
$webform_renderable = webform_submission_render($webform_node, $submission, '[email protected]', 'html');
$content[] = drupal_render($webform_renderable);
}
}
return $content;
}
My PDF body content custom PHP code (requires Core PHP input being enabled) is as follows:
<?php
// path of a submission PDF download is of the format:
// node/1/submission/1/downloadpdf
$path = current_path();
$parts = explode('/', $path);
$sid = $parts[3];
$content = _MYMODULE_render_reference_submissions(array($sid));
return $content[0];
?>
Webform submissions can be rendered as like plaintext
, html
and something else I think out of the box via the $format
value passed into webform_submission_render
.