Hello everyone,

in the previous version i used to have a webform-mail.tpl.php in which the following code allowed me to only send the non empty fields of the webform :

 if (!function_exists('webform_empty_fields')) {
    function webform_empty_fields(&$form_values) {
      foreach ($form_values as $key => $value) {
        if ($value == '') {
          unset($form_values[$key]);
        }
        if (is_array($value)) {
          webform_empty_fields($form_values[$key]);
        }
      }
    }
  }
webform_empty_fields($form_values['submitted_tree']);
print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);

how can i get something similar with 6.3 version ?

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

You can do this by theming the theme_webform_element_text() function in your template.php file.

function mytheme_webform_element_text($element, $value) {
  // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
    return '';
  }
  // Call the default theme function if there is a value.
  return theme_webform_element_text($element, $value);
}
SophieG’s picture

Thanks !! it works perfectly ! But just for one webform.... Any idea why ?

quicksketch’s picture

That should affect all Webforms on your site, since all Webform e-mails go through that function.

quicksketch’s picture

Status: Active » Fixed

Marking fixed, please reopen if you have another question about this issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

RichieRich’s picture

I must be doing something wrong but I simply can't get this to work at all. For test purposes I defined the function as so but even then everything is being printed as usual:

function ishigaki_webform_element_text($element, $value) {
    return '';
}

My theme is naturally called 'ishigaki'. Is there anything else that I have to add to the template.php file to make this function be called as it's clearly having no affect at the moment.

Update: I'm using version 6.27, perhaps I'm trying to override a function which isn't even available!

sachariew’s picture

Version: 6.x-3.4 » 6.x-3.0-beta2
Priority: Minor » Normal
Status: Active » Closed (fixed)

Hi,

I know that I shouldn't try to mess with things that I don't understand or can't control, but I was very much interested in a more complete solution on how to include only the non-empty fields in the email. After (re-)reading the comments on this issue I found out how to do it. At last. Now here's a dummys guide to displaying only the non-empty fields in the email!

1. Create a template.php file in your theme's directory.

2. Paste the code from comment no. 1 into it.

3. Change the name of the function in the code from "mytheme" to your themes name. (I assume this should be case sensitive?)

4. Save the template.php and clear the sites caches. (Additionally: visit the theme select page at "Administer > Site building > Themes" ?)

That's it!

PS: quicksketch - this module is amazing and ver. 3 is even better than before. Thanks for your work!!!

sachariew’s picture

Version: 6.x-3.0-beta2 » 6.x-3.1
Priority: Normal » Minor
Status: Closed (fixed) » Active
akalata’s picture

Category: support » feature

Changing this to a feature request.

@RichieRich, did you try the code provided in the OP? I tried adding #1 to an install of 6.x-2.9, and it didn't appear to work, so I'm guessing that it's 6.x-3.x only.

hiweed’s picture

Version: 6.x-3.1 » 6.x-3.2

Thank you, quicksketch! This works great with 6.x-3.2.

I also want to "Display only the non empty fields in the Submission page". I know I can copy templates/webform-submission-page.tpl.php to my theme's folder and customize it, but how?

Thank you in advance!

LudachrisGSX’s picture

I tried using the code supplied by the original poster and the code in the first comment, and it is working - the email is coming through in the email, and only the non-empty fields are being displayed in the email. But, I'm receiving an error upon submission on the returned page of the form. It's telling me this:

    * warning: Invalid argument supplied for foreach() in /home/site/public_html/sites/dev.site2.com/themes/inf08/webform-mail.tpl.php on line 42.
    * warning: Invalid argument supplied for foreach() in /home/site/public_html/sites/dev.site2.com/themes/inf08/webform-mail.tpl.php on line 42.
    * warning: Invalid argument supplied for foreach() in /home/site/public_html/sites/dev.site2.com/themes/inf08/webform-mail.tpl.php on line 42.

And line 42 has this code in it:

foreach ($form_values as $key => $value) {
jh3’s picture

@LudachrisGSX

If you're using D6 and a newer version of webform, I suggest using quicksketch's solution (post #1).

Also, the foreach statement is probably giving you warnings because the array being passed is empty at some point during loop.

jpsimon’s picture

Version: 6.x-3.2 » 6.x-3.4
Category: feature » support

this worked to hide empty form fields in the email receipt in version 6.x-3.2

but now that I'm in 3.4 it doesn't work... Is anyone able to help me? thanks!

function mytheme_webform_element_text($element, $value) {
 // Check if there is any value to print out at all, if not, return an empty string.
 if (strlen(trim($value)) == 0) {
   return '';
 }
 // Call the default theme function if there is a value.
 return mytheme_webform_element_text($element, $value);
}
jh3’s picture

@jpsimon

Do not prefix _webform_element_text with your theme name; use theme_webform_element_text.

You should end up with:

function YOURTHEME_webform_element_text($element, $value) {
  if (strlen(trim($value)) == 0) {
    return '';
  }

  return theme_webform_element_text($element, $value);
}  
jpsimon’s picture

Version: 6.x-3.0-beta2 » 6.x-3.4
Priority: Normal » Minor
Status: Closed (fixed) » Active

thank you so much, that worked like a charm!!

breeze76’s picture

Forgive me, but I can not seem to get this to work... I copied my "webform-mail.tpl.php" into the current theme directory I am using. Added the code from #1 into the template.php file. One question though, do I need the <?php and ?> at the end of the code? At the beginning of the template.php, it already has the <?php but there wasn't a ?> at the end before I put the code. So, does it suppose to have the ?> at the end?

Sorry new at all this..

Gene

jh3’s picture

@breeze76

You should not need to copy the webform-mail.tpl.php file into your theme's directory unless you plan on making changes to it.

You do not need to copy the php tags that are in comment #1. Just copy the function and paste it at the bottom of your template.php file. And it's a best practice to omit the closing php tag. It helps with preventing unintentional trailing white space. Check out http://drupal.org/coding-standards

Finally, be sure to change mytheme to the name of your theme! Then save the file, clear your cache, and things should work.

akalata’s picture

Here's #1 updated for D7

function THEMENAME_webform_element_text($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];
  
	// Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
    return '';
  }
  // Call the default theme function if there is a value.
  return theme_webform_element_text($variables);
}

jg314’s picture

I was having a major issue because I couldn't figure out how to remove select labels from emails when nothing was selected. I tried the method given in comment 1 (http://drupal.org/node/749360#comment-2751838), but had no luck. Then I figured out how to do it using theme_webform_display_select($element). I used this code since I only wanted to do it on a specific group of forms and only in html emails.

<?php
  //Added a special if statement here to add bullet points for only one selected item and remove labels for items with no selections.
  if ($element['#format'] == 'html' && isset($element['#webform_component']['extra']['items']) && substr($element['#webform_component']['extra']['items'],0,9) == "meal|Meal"){
   $output = count($items) >= 1 ? theme('item_list', $items) : '';
  }
?>

The key being that if $items is less than 1 the output is ''. That way nothing, including the label, will be displayed in the email.

Please also note that the code snippet above is only one piece of that entire function.

breeze76’s picture

OK.. I know this was an old post, but finally getting back to site...

I copied the code at the bottom of my webform-mail.tpl.php file. Did not use the PHP wrappers either. Changed the "mytheme" to the theme that I am using. I cleared cache and submitted form, but still get the the empty fields. I am using the latest version of webform.

Any ideas on what I am doing wrong?

akalata’s picture

@breeze76, the code snippets we've been discussing should go in the template.php file for your theme -- if it's not there, you will need to create one. You shouldn't need to copy the webform-mail.tpl.php file unless you're making other changes (as mentioned in #17). Also, make sure you're using the right version of the code (D6 vs. D7).

quicksketch’s picture

Version: 6.x-3.4 » 7.x-3.x-dev
Category: support » feature
Priority: Minor » Normal

Just as a potentially crazy idea, wouldn't it be nice to just add this as an option in the e-mail configuration options? Seems like we could toss it into the "Included e-mail values" fieldset, as we get this request all the time.

breeze76’s picture

akalata,

It was already in me template.php file as such:

/*
Display only the non empty fields in the email
*/
function danland_webform_element_text($element, $value) {
  // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
    return '';
  }
  // Call the default theme function if there is a value.
  return theme_webform_element_text($element, $value);
}

But still does not work... am I missing something else somewhere?

breeze76’s picture

I would second that... since it would make it easier for me to get this to work... :)

jg314’s picture

@breeze76, I didn't have a ton of time to look at your code, but you could be having the same problem I ran into. Take a look at my comment at http://drupal.org/node/749360#comment-4714046. Hopefully that will be helpful for you.

@quicksketch, making this an option from the backend sounds like an awesome idea.

breeze76’s picture

jg314, Does the below code go in the template.php file as well? and should I remove the other code? What is "meal|MEAL" mean?

<?php
  //Added a special if statement here to add bullet points for only one selected item and remove labels for items with no selections.
  if ($element['#format'] == 'html' && isset($element['#webform_component']['extra']['items']) && substr($element['#webform_component']['extra']['items'],0,9) == "meal|Meal"){
   $output = count($items) >= 1 ? theme('item_list', $items) : '';
  }
?>

Thanks for your time in helping me figure this out...

jg314’s picture

@breeze76, the code that you copied is a piece of the much larger theme_webform_display_select() function. You need to copy that function into your template.php file and adjust it however you'd like. I definitely wouldn't use the code I provided verbatim, because it is for a very particular group of forms on a website we manage. Also, that function only pertains to select fields like checkboxes. Really, it should be used only as a starting point to help you.

breeze76’s picture

Since I STILL can not get this to work... I was looking, do I need to have the input format for MIME Mail set to Filtered or Full HTML?

vernond’s picture

Status: Active » Needs review
FileSize
4.66 KB
3.7 KB

I've added a checkbox for excluding empty components to the email configuration options. It was necessary to change the look 'n feel a little by adding a wrapper fieldset to avoid interference from the javascript magic that drives the "Include all components" functionality.

Patches for Webform 6 & 7 attached.

timgilmour’s picture

patch in #29 works great -- only thing, only seems to work with textfields / textareas, not with selects (haven't tried other component types).

1kenthomas’s picture

As I just emailed Tim--

I think it's going to be fairly hard to have a general purpose response to any kind of field that could be added to the webform (maybe I'm wrong!). But certainly custom fields are going to have various data structures, and it is hard to anticipate what kind of output will be appropriate to send to the "end user."

What we should be working for then, may be more of a system to handle different kinds of fields and present appropriate options.

kaido.toomingas’s picture

I did like that just a minute ago...
Copied webform-mail.tpl.php into my theme folder and renamed this to webform-mail-nid.tpl.php template.
nid = Current node id.
After that I just replaced %email_values with

for($i=1; $i <= count($node->webform['components']); $i++) {
$key = $node->webform['components'][$i]['form_key'];
 if(!empty($_POST['submitted'][$key])) {
     echo $node->webform['components'][$i]['name'] . ": "; 
     echo "%value[$key]" . " \n"; 
  }
} 

mhh seems this isnt to right place for this. I did this one for a d6... Still d7 template looks the same.. Maybe this will work for d7 also.

Akshita’s picture

Priority: Normal » Major

Drupal -7 (webform :7.x-3.13)

Please do the needful -I did followed your instructions but not working.

I have created a webform-mail-393.tpl.php (/var/www/profiles/openpublic/themes/openpublic_theme/)

Cleared the cache

Filled the form (node id : 393) with some empty fields and submitted the form but the email I received also contains non empty fields with jsut labels (no values)

Akshita’s picture

I know how to patch a module. Can you please help me how to patch only a particular file (/include/) of amodule.

My problem is not resolved with all other solutions found on the page.

Problem : Email Message contains empty fields also

Please do the needful.

Thanks in advance

Sheldon Rampton’s picture

Here's a solution that can go in a webform-mail-nid.tpl.php file:

$parents = array();
foreach ($submission->data as $cid => $value) {

  if ($value['value'][0] == '0') {
    $email['excluded_components'][] = $cid;
    if (!isset($parents[$node->webform['components'][$cid]['pid']])) {
      $parents[$node->webform['components'][$cid]['pid']] = 0;
    }
  }
  else {
    $parents[$node->webform['components'][$cid]['pid']] = 1;
  }
}
foreach($parents as $pid => $has_requests) {
  if (!$has_requests) {
    $email['excluded_components'][] = $pid;
  }
}
$email_values = webform_submission_render($node, $submission, $email, 'text');
echo "Submitted on %date

Submitted values are:
$email_values

The results of this submission may be viewed at:
%submission_url
";
seworthi’s picture

I'm using webform 6.x-3.15, and applied the patch in #29.

I worked great, except the comments in #30. I changed the following from the patch (webform.submissions.inc -> @@ -539,6 +541,15 @@ function webform_submission_render($node, $submission, $email, $format) {). This check if any component on the form, not excluded, has a value. If not, it removes the component.

Works on selects, markup, text / textareas. Should also work on custom fields.

<?php
// Remove empty components.
if ($exclude_empty) {
  foreach($components as $cid => $data) {
    // Continue if field is a fieldset, because it will have no submitted data
    if ($components[$cid]['type'] == 'fieldset') {
      continue;     
    }
    // See if field has submitted data, and data not empty
    if (!isset($submission->data[$cid]['value'][0]) || $submission->data[$cid]['value'][0] == '') {
      unset($components[$cid]);
    }
  }
}
?>
dalberts69’s picture

#35 - this worked for me, modifying the if value = 0 line

if ($value['value'][0] == '0' || empty($value['value'][0])) {
quicksketch’s picture

Thanks @vernond for the patch! I'll review this when I get the chance. Looks excellent at first blush. :)

Akshita’s picture

Title: Display only the non empty fields in the email » Query String Disappears for mega menu
Project: Webform » Drupal core
Version: 7.x-3.x-dev » 7.0
Component: Miscellaneous » menu system
Category: feature » bug
Priority: Major » Critical

Sorry this is not the right thread.looking forward for some help

Here is the situation:

I have an issue with Mega menu. I have a redirect script (customized) which works fine on the right nav elements of any node.

But from the mega menu the query string disappears.

For example : Mega Menu (Main menu from the home page)

Divisions(Main Tab) and on click I have drop down

Divisions->D1 -D2 (If I use redirect script on d2 like (redirect?path=abcd) then when I try to access the link D2 then URL shows only http://mydomain.com/redirect)

Here is my code:

/* Add a system path that we can use to redirect content */

function my_custom_menu() {
$items['redirect'] = array(
'title' => 'Perform Redirect',
'description' => '',
'page callback' => 'my_custom_redirect',
'page arguments' => array(''),
'access arguments' => array('access content')
);
return $items;
}

function my_custom_redirect() {
$path = $_GET['path'];
header("Location: $path");
}

Any Help is appreciated.

Thanks in advance

Status: Needs review » Needs work

The last submitted patch, webform-7-emailnonempty-749360-22.patch, failed testing.

JStanton’s picture

Project: Drupal core » Megamenu
Version: 7.0 » 7.x-1.x-dev
Component: menu system » Code
Priority: Critical » Normal
Hanno’s picture

Title: Query String Disappears for mega menu » Display only the non empty fields in the email
Project: Megamenu » Webform
Version: 7.x-1.x-dev » 7.x-3.x-dev
Category: bug » feature
Status: Needs work » Needs review

Well, this issue moved by accident to the wrong issue queue, bakc to webform :)

quicksketch’s picture

Thanks @Hanno.

Akshita’s picture

Hi Sheldon

$parents = array();
foreach ($submission->data as $cid => $value) {

  if ($value['value'][0] == '0') {
    $email['excluded_components'][] = $cid;
    if (!isset($parents[$node->webform['components'][$cid]['pid']])) {
      $parents[$node->webform['components'][$cid]['pid']] = 0;
    }
  }
  else {
    $parents[$node->webform['components'][$cid]['pid']] = 1;
  }
}
foreach($parents as $pid => $has_requests) {
  if (!$has_requests) {
    $email['excluded_components'][] = $pid;
  }
}
$email_values = webform_submission_render($node, $submission, $email, 'text');
echo "Submitted on %date

Submitted values are:
$email_values

The results of this submission may be viewed at:
%submission_url
";

Can you pleas help me in customizing the titles(field title) with html tags and a line break between the fields.

Sheldon Rampton’s picture

@Akshita Can you explain what you need? I need more detail.

Akshita’s picture

Hi Sheldon

I am sending 5 fields from the webform as email.

First Name
Last Name
Address1
Email
Phone

since we are using $email_values all the fields are sent l -thats good.How do I send each field with bold format (only titles of the field) and a line break between each.

Please do respond if any other info is required.

Please do the needful. Help appreciated.

Thanks

Sheldon Rampton’s picture

Akshita, can you post a sample of the HTML that is currently being generated? I think you can probably do what you want by just adding some CSS to your email, or else maybe by doing a search-and-replace to insert some HTML tags into the HTML that gets generated by the webform_submission_render function.

Akshita’s picture

Hi Sheldon

Thanks for the quick turn around - Appreciated

Here is the sample of email:

Submitted on Thu, 03/22/2012 - 12:38

Submitted values are:

--Personal Information:--
First Name: Test123
Last Name: Test
Address 1: Test
Address 2: t
City: Fremont
State/Province: California
Zip/Postal Code: 12345
Phone: (860) 123-3456
Email Address: akshitab@testmail.com

--Comments:--
Comments: This is a test

How can I add HTMl tags in the email?. the code I am using to send only non empty fields is as follows:

$parents = array();
foreach ($submission->data as $cid => $value) {

  if ($value['value'][0] == '0') {
    $email['excluded_components'][] = $cid;
    if (!isset($parents[$node->webform['components'][$cid]['pid']])) {
      $parents[$node->webform['components'][$cid]['pid']] = 0;
    }
  }
  else {
    $parents[$node->webform['components'][$cid]['pid']] = 1;
  }
}
foreach($parents as $pid => $has_requests) {
  if (!$has_requests) {
    $email['excluded_components'][] = $pid;
  }
}
$email_values = webform_submission_render($node, $submission, $email, 'text');
echo "Submitted on %date

Submitted values are:
$email_values

The results of this submission may be viewed at:
%submission_url
";
le dendrite’s picture

Hi, I could use some help getting this correct. I'm looking to do something slightly different.

My form is something like this,

-fieldset (non editable) Supplier A
--fieldset (non editable) Item A
--textfield (user input) Order Quantity

--fieldset (non editable) Item B
--textfield (user input) Order Quantity

-fieldset (non editable) Supplier B
--fieldset (non editable) Item A
--textfield (user input) Order Quantity

--fieldset (non editable) Item B
--textfield (user input) Order Quantity

...and so on.

My Field Key scheme is,
supplier_SUPPLIER NAME
item_SUPPLIER NAME_ITEM NAME

What i'm looking to accomplish is to omit from the email not just fields with no input, but also all fields related to fields with no input.

So the email looks like this,
-Supplier A
--Item B
--Order Quantity: 4

--Item D
--Order Quantity: 12

And not like this,
-Supplier A
--item A

--Item B
--Order Quantity: 4

--Item C

--Item D
--Order Quantity: 12

Any help on how to accomplish this is much appreciated.
Thanks

vernond’s picture

@le taco see THEMING.txt in Webform folder for info on customising email output. You're going to need to code up a solution that conditionally includes/excludes fields.

kiwimind’s picture

Having applied the patch in #29 against the latest dev, emails are still being sent with all fields, whether they have a value or not.

I have checked that the settings within the email area have the box checked to exclude empty fields.

bbzed’s picture

Version: 7.x-3.x-dev » 6.x-3.18
Category: feature » support

Im using 6.x-3.18. Ive created template.php in my themes root folder and pasted the code from #1 and changed the function to - "function fusion_mayan_webform_element_text($element, $value) " (my theme name).

The emails still have all the empty field labels included.
All my fields are in fieldsets.

Am i doing something wrong?

le dendrite’s picture

Sheldon's #35 with dalberts69's #37 accomplished the email results I need, but leaves me with errors.

at webform/emails

warning: Invalid argument supplied for foreach() in /home/SITENAME/public_html/new/sites/default/themes/THEMENAME/webform-mail-NID.tpl.php on line 24.

24. foreach ($submission->data as $cid => $value) {

and at webform/emails edit

Fatal error: Call to undefined function webform_submission_render() in /home/SITENAME/public_html/new/sites/default/themes/THEMENAME/webform-mail-NID.tpl.php on line 41

41. $email_values = webform_submission_render($node, $submission, $email, 'text');

Quicksketch's #1 is working great without any errors.

Thanks.

dflitner’s picture

akalata's #18 is working great for me (so far) with Drupal 7.15 and Webform 7.x-4.0-alpha6.

Thanks

Akshita’s picture

Hi Dogmatix

I am using Drupal 7.15 and Webform 7.x-4.0-alpha6

Here's a solution that I used in a webform-mail-nid.tpl.php file to display only non empty fields in the email but the below solution is not working with Webform 7.x-4.0-alpha6:

<?php
$parents = array();
foreach ($submission->data as $cid => $value) {

  if ($value['value'][0] == '0') {
    $email['excluded_components'][] = $cid;
    if (!isset($parents[$node->webform['components'][$cid]['pid']])) {
      $parents[$node->webform['components'][$cid]['pid']] = 0;
    }
  }
  else {
    $parents[$node->webform['components'][$cid]['pid']] = 1;
  }
}
foreach($parents as $pid => $has_requests) {
  if (!$has_requests) {
    $email['excluded_components'][] = $pid;
  }
}
$email_values = webform_submission_render($node, $submission, $email, 'text');
echo "Submitted on %date

Submitted values are:
$email_values

The results of this submission may be viewed at:
%submission_url
";
?>

Could you please help me how you have resolved this issue with Webform 7.x-4.0-alpha6 ?

You have mentioned that this issue resolved with the below solution provided by #18 Akalata

<?php
function THEMENAME_webform_element_text($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];
  
    // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
    return '';
  }
  // Call the default theme function if there is a value.
  return theme_webform_element_text($variables);
}
?>

Please let me know where did you place the above code and how you got it resolved? What the THEMENAME in the above code refers to?

Please provide the steps .

Thanks

Akshita’s picture

Hi Sheldon

I am using Drupal 7.15 and Webform 7.x-4.0-alpha6

Here's a solution (provided by you only (#35)) that used in a webform-mail-nid.tpl.php file to display only non empty fields in the email but the below solution is not working with Webform 7.x-4.0-alpha6:

<?php
$parents = array();
foreach ($submission->data as $cid => $value) {

  if ($value['value'][0] == '0') {
    $email['excluded_components'][] = $cid;
    if (!isset($parents[$node->webform['components'][$cid]['pid']])) {
      $parents[$node->webform['components'][$cid]['pid']] = 0;
    }
  }
  else {
    $parents[$node->webform['components'][$cid]['pid']] = 1;
  }
}
foreach($parents as $pid => $has_requests) {
  if (!$has_requests) {
    $email['excluded_components'][] = $pid;
  }
}
$email_values = webform_submission_render($node, $submission, $email, 'text');
echo "Submitted on %date

Submitted values are:
$email_values

The results of this submission may be viewed at:
%submission_url
";
?>

The above code is no more working with webform 7.x-4.0-alpha6 -could you please help me with this?

Any help is much appreciated.

dflitner’s picture

Akshita,

I just tested that this still works and it does. (Drupal 7.16 and Webform 7.x-4.0-alpha6)

Place this code:

function THEMENAME_webform_element_text($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];
  
    // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
    return '';
  }
  // Call the default theme function if there is a value.
  return theme_webform_element_text($variables);
}

Into the template.php file in your theme directory (probably sites/all/themes/yourtheme). Do not include an opening or closing php tag around this function. Replace THEMENAME with the name of your theme as it appears on the theme folder.

Clear the cache on your site and it should work.

Akshita’s picture

FileSize
5.61 KB

Thanks!

Version of Drupal is 7.14 and Webform 7.x-4.0-alpha6

Yes I did the same way. I have 20 webforms out of which 10 will send the email as HTML so I have Installed MIME MAIL MOdule.

Not sure whether this is restricting?

Here is the code :

function openpublic_theme_webform_element_text($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];

    // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
   return '';
  }
 // Call the default theme function if there is a value.
  return theme_webform_element_text($variables);
}

I am attaching template.php (/var/www/profiles/openpublic/themes/openpublic_theme/template.php) as template.txt-Please check it.

I also tried replacing the above code with the following :

function openpublic_theme_webform_element($variables) {
  $element = $variables['element'];
  $value = $variables['element']['#children'];

    // Check if there is any value to print out at all, if not, return an empty string.
  if (strlen(trim($value)) == 0) {
   return '';
  }
 // Call the default theme function if there is a value.
  return theme_webform_element($variables);
}
Akshita’s picture

Never mind.

I resolved my issue. I deleted all the default values on the fields and the code in the template.php worked.

thanks again all

twixo’s picture

Version: 6.x-3.18 » 7.x-3.17

I run Drupal 7 and Webform 3.17. I´m not very good at drupal or PHP. I have manage to make a webform and it works fine, but when sending the mail all the fields are shown. I want to remove non empty fields in the email. I understand I can change a template to customize the outgoing mail. But how? I can see some php code in this tread. Can I use the code whitout any further customizing and where to put the code?

Thanks

Anonymous’s picture

I hate to bring up an old issue again, but I have been trying to get this to work. I have added the following to my \themes\pixture_reloaded\template.php file:

function pixture_reloaded_webform_element_text($variables) {
$element = $variables['element'];
$value = $variables['element']['#children'];

// Check if there is any value to print out at all, if not, return an empty string.
if (strlen(trim($value)) == 0) {
return '';
}
// Call the default theme function if there is a value.
return theme_webform_element_text($variables);
}

However, it still lists every component, whether it has a value or not. I did flush my cache after adding this to template.php. Am I missing something?

kaido.toomingas’s picture

Anonymous’s picture

Thanks Akshita #58 that worked!

gr8britton I also restarted apache and it worked for me. You can try that.

Anonymous’s picture

I'll give that a shot. Thank you.

quicksketch’s picture

Version: 7.x-3.17 » 7.x-4.x-dev
Category: support » feature
Status: Needs review » Active

I would still like to see this as a built-in feature of Webform. I think it would be a useful feature to simply add a checkbox to the e-mail configuration that states "show only filled out fields" when you set up the e-mail. However like all features this would only be added to the 4.x version at this point.

If you guys have further questions on how to do this via theming, please keep the discussion going here, however please don't change the category/status/version on the issue.

vmed’s picture

confirm #57 works for me, on Drupal 7.16 and Webform 7.x-4.0-alpha6

johan

Gerard’s picture

#57 works except for textarea fields. Labels of empty text area fields are shown in the mail.
And select lists with multiple selections, when nothing is selected in the mail the fieldname is printed: [fieldname]

Jana1701’s picture

Issue summary: View changes

This is seriously driving me crazy and I cannot get it to work. I've spent days on this issue. Does anyone have a current solution?

dflitner’s picture

Can you describe your problem a little more, Jana1701?

I'm on Webform 7.x-4.0-beta3 and Drupal 7.26 using the same code from #57 and it's still working.

Blank fields (including multiple selection) don't appear in the email while filled in fields do.

labboy0276’s picture

OK

So I had a similar problem as everyone has on this discussion. I have a client who uses HTML module to send their submission data, etc etc etc.

I tried the code in #57 and it did not work. It seems that the code would still send a blank result based on this line:

if (strlen(trim($value)) == 0) {
   return '';
  }

You are still sending an empty string based off a length of zero. Still not what is needed. What I need (and what some people seem to need) is to just not return the result at all if you have no data in the field.

SO

I stared at the API for the webforms here: http://drupalcontrib.org/api/drupal/contributions!webform!webform.api.ph...

I then saw this hook:
http://drupalcontrib.org/api/drupal/contributions%21webform%21webform.ap...

So in my feature (or your module) I did some codez and came up with this:

/**
 * Implements hook_webform_submission_render_alter().
 */
function YOURMODULE_webform_submission_render_alter(&$renderable) {
  foreach (element_children($renderable) as $key) {
    //textfields
    if ($renderable[$key]['#value'] == '') { 
      unset($renderable[$key]);
    //select lists, checkboxes, etc.
    } elseif (is_array($renderable[$key]['#value'])) { 
      if ($renderable[$key]['#value'][0] == '') {
        unset($renderable[$key]);
      }
    }
  }
}

And this now no longer sends a result in the emails when there is no result AND it also doesnt send the key, so you dont have an empty result.

Hopefully this helps some people out there.

penone’s picture

Hi Labboy.

Tried your fix but am getting notices that say "Notice: Undefined index: #value in emptyemailfix_webform_submission_render_alter() (line 8".

Am I missing something?

Thanks!

labboy0276’s picture

Hi penone

You could just try adding an isset, but not sure how you pasted the code, which codeline is line 8 on your end?

hosais’s picture

Lobboy0276. Thanks you for the sharing. It works great in my case (especially in my case, all the items are required, so there is no NULL such thing).

hosais

adferris’s picture

#70 worked for me although I also needed to handle fields inside a fieldset. This is my solution.

// We also need to handle fieldsets
 if (isset($renderable[$key]['#type']) && $renderable[$key]['#type'] === 'fieldset') {
   $hasfield = FALSE;
   foreach (element_children($renderable[$key]) as $key2) {
       // If the textfield is empty
       if (isset($renderable[$key][$key2]['#value'])) {
         if ($renderable[$key][$key2]['#value'] == '') {
           unset($renderable[$key][$key2]);
         // If select lists, checkboxes, etc. are empty
         } elseif (isset($renderable[$key][$key2]['#value'][0]) && $renderable[$key][$key2]['#value'][0] == '') {
             unset($renderable[$key][$key2]);
         } else {
           $hasfield = TRUE;
         }
      }
    }
    // If the fieldset has no field, remove it.
   if ($hasfield == FALSE) {
     unset($renderable[$key]);
   }
 }
bneil’s picture

I'm interested in working on a patch to provide the configuration mentioned by quicksketch in #65. Where in the submission and email render process might be the best place to do this?

DanChadwick’s picture

@bneil -- It seems that there are really two slightly different requests here.

  1. When a component has either no value or an empty value, don't display it at all. This would imply that its label isn't shown on the submission page where a completed submission is shown. It might even further be extended to views support (but I'm not suggesting that). Perhaps the token would not be replaced (avoiding the display of the label if ":withlabel" is used). And definitely the component would be omitted from an e-mail if is is included as a component on that e-mail. Note that a default value would not be considered empty.
  2. The other request is just to do the e-mail part.

In the former case, we'd be adding an option to every printable component to indicate whether it should be hidden when blank/empty. In the later case we'd be adding an option to the e-mail definition.

I think I favor 2).

ecvandenberg’s picture

I notice some different behavior in handling the e-mail template token values. I just noticed it so I'm not sure when this occurred. At this moment I use webform 7.x-4.1. Perhaps the upgrade from 7.x-3.x caused the different behavior.

I used the token [submission:nl:?] for printing option-lists. When no option was selected, the field was not printed. Even the label was not printed. That was just what I needed.

And now when no option is selected the token is printed. So i.e. [submission:nl:mykey]. When one or more options are selected, the value is nicely printed.

If this info helps with implementing this request and more detailed info is needed, I'm willing to restore an older version of my site and see what caused the different behavior. Just let me know.

DanChadwick’s picture

Re #77 -- that's a different issue. This issue is about whether empty components are printed in the template, not via tokens. Try the latest dev. There is an issue in 4.7.

DanChadwick’s picture

Status: Active » Fixed
FileSize
6.91 KB

Alright, I took pity on this 5 year old feature request. Maybe someone will fund this retroactively. :)

While I appreciate the creativity of the approach taken in #29, I think a more straight-forward storing of the option in the webform_emails table is better for the long run.

This option only hides empty components from the [submissions:values] token when used in an e-mail.

Committed to 7.x-4.x.

Be sure to run update.php / drush updatedb

DanChadwick’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Status: Fixed » Patch (to be ported)

  • DanChadwick committed 897f62a on 7.x-4.x
    Issue #749360 by vernond, DanChadwick: Display only the non empty fields...
srutland’s picture

Grabbed the patch with wget https://www.drupal.org/files/issues/webform-empty_component_token-749360...

In trying to apply the patch, got trailing whitespace error and patch would not apply:

$ git apply webform-empty_component_token-749360-79.patch
webform/webform-empty_component_token-749360-79.patch:115: trailing whitespace.
warning: 1 line adds whitespace errors.

$ git apply --whitespace=fix webform-empty_component_token-749360-79.patch
webform/webform-empty_component_token-749360-79.patch:115: trailing whitespace.
warning: 1 line adds whitespace errors.

$ git apply --ignore-space-change --ignore-whitespace webform-empty_component_token-749360-79.patch
webform/webform-empty_component_token-749360-79.patch:115: trailing whitespace.
warning: 1 line adds whitespace errors.

DanChadwick’s picture

Geez. I've set my IDE and git to avoid trailing white space, but seem to still be having trouble. Minor issue.

Note that this is committed. It might be easier for you to check out the latest dev via git or wait for the d.o packaging to make a new -dev release.

srutland’s picture

Hmm, that makes more sense. Thanks for the tip.

$ drush dl webform-7.x-4.x-dev
File webform-7.x-4.x-dev.tar.gz_date=1429303381 is corrupt (wrong md5 checksum).

Instead did a wget on the dev version, untarred it, updated the db and it seems to work fine. Haven't thoroughly vetted it though.

DanChadwick’s picture

I'm pretty sure that just means that drush is out-of-date. You can try:
drush pm-referesh
Sometimes I've had to truncate the contents of the update cache table.

Or I might be mis-remembering. Everything that happens, I have to search....

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Status: Patch (to be ported) » Fixed

Committed and pushed to 8.x-4.x. Thanks!

  • fenstrat committed 1070577 on 8.x-4.x
    Issue #749360 by vernond, DanChadwick: Display only the non empty fields...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Funksmaname’s picture

I just put this feature to the test, and when using Fieldsets the field set title appears even if all of it's contained children are hidden. Is there a way to make these also hidden if no sub fields are filled in?

Thanks, and let me know for future if protocol is to open a new issue for this sort of thing? ;)

petednz’s picture

Thanks for the work on this Dan - and your comment "Maybe someone will fund this retroactively" is noted.

EDIT: finally spotted that within INCLUDED E-MAIL VALUES section there is an option for "Exclude empty components"

(removed all the details as no point having it here)