diff --git a/includes/webform.submissions.inc b/includes/webform.submissions.inc index b715e84..41c78ce 100644 --- a/includes/webform.submissions.inc +++ b/includes/webform.submissions.inc @@ -649,19 +649,15 @@ function webform_submission_render($node, $submission, $email, $format, $exclude foreach ($excluded_components as $cid) { unset($components[$cid]); } - if (!empty($email['exclude_empty'])) { - foreach ($submission->data as $cid => $data) { - // Caution. Grids store their data in an array index by question key. - if (implode($data) == '') { - unset($components[$cid]); - } - } - } } module_load_include('inc', 'webform', 'includes/webform.components'); _webform_components_tree_build($components, $component_tree, 0, $page_count); + if (!empty($email['exclude_empty'])) { + _webform_submission_remove_empty_components($submission->data, $components, $component_tree); + } + // Make sure at least one field is available if (isset($component_tree['children'])) { // Recursively add components to the form. @@ -679,6 +675,32 @@ function webform_submission_render($node, $submission, $email, $format, $exclude } /** + * Remove all components without values and all fieldsets without children. + * + * @param $data + * Submission data from webform_submission_render(). + * @param $components + * Components from webform_submission_render(). + * @param $tree + * Component tree or child tree from webform_submission_render() or recursion. + * @return + * TRUE: tree doesn't have children. + * FALSE: tree has children. + */ +function _webform_submission_remove_empty_components($data, &$components, &$tree) { + foreach ($tree['children'] as $key => $child) { + if (!isset($data[$child['cid']]) || implode($data[$key]) == '') { + // TODO Add hook so modules can implement more form element types with children. + if ($child['type'] != 'fieldset' || _webform_submission_remove_empty_components($data, $components, $tree['children'][$key])) { + unset($tree['children'][$key]); + unset($components[$key]); + } + } + } + return count($tree['children']) == 0; +} + +/** * Return all the submissions for a particular node. * * @param $filters