Hi,
is it possible to exclude empty fields from email?
I have a lot of fields in my form - and the text-email becomes very user-unfriendly, as all empty fields are also stored in the email.
Is there a way to stop this?
I am already working with a custom email-template - but did not find out, what to include.

Regards

vistee

Comments

quicksketch’s picture

Category: task » support

In your custom e-mail template, you can add a few lines to remove components that do not have any value. You'll just need to expand on the example already provided in the .tpl.php file.

In your webform-mail.tpl.php that you've copied to your theme directory change lines 38 through 43 to:

  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
  // unset($form_values['submitted_tree']['element_key']);
  foreach ($form_values['submitted_tree'] as $key => $value) {
    if ($value == '') {
      unset($form_values['submitted_tree'][$key]);
    }
  }

  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);
vistree’s picture

Hi quicksketch,
thank you very much for this fast reply.
I tried your code out - but it does not work.
The result is the same as without the foreach-loop.
Is there something still not correct, or can it have todo with my 6.2.3-version of webforms?

Kind regards

vistree

vistree’s picture

Ah yes, and I use a lot of fieldsets and also multipage-layout of my webform.

Regards

vistree

quicksketch’s picture

Right... in which case you'll need to do multiple loops (or recursive looping) to remove all the empty values inside of fieldsets. The approach I used above would only work if you didn't use any fieldsets. Multiple pages do not make a difference though when dealing with e-mails.

vistree’s picture

Ah OK,
How can I loop through the fieldset than? Is the resulting value of

  foreach ($form_values['submitted_tree'] as $key => $value) {

the fieldset?
And can I just loop again like

  foreach ($values['submitted_tree'] as $key => $value2)

I have NO idea on php. So I would be very glad to have help again.

Best regards

vistree

vistree’s picture

Ok, now I tried to play a little bit with the possible values and found out, that the value of a fieldset is 'Array'.
Now I thought, that it could be quite easy to generate an additional loop. But my code still does not work:

  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
  // unset($form_values['submitted_tree']['element_key']);
  foreach ($form_values['submitted_tree'] as $key => $value) {
    if ($value == '') {
      unset($form_values['submitted_tree'][$key]);
    }
    elseif ($value == 'Array') {
         foreach ($form_values['submitted_tree'] as $key => $value) {
		    if ($value == '') {
      			unset($form_values['submitted_tree'][$key]);
      		}
    }

    }
  }
  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);

Can anyone help me with the additional loop?

Kind regards

vistree

vistree’s picture

I still search for a solution.
Is there anyone fit in PHP and can explain how to also remove the empty fields from fieldset?

Hope for help ;-)

Thanks and regards

vistree

quicksketch’s picture

Something like this should be pretty close. Again, not tested.

  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
  // unset($form_values['submitted_tree']['element_key']);
  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);
vistree’s picture

Hi quicksketch,
this works absolutly perfect!!!!
Thank you so much!!!

Last question on this: is it also possible to remove the name of the fieldset (the fieldset title) if the fieldset is empty??

Kind regards and again 1000 thanks

vistree

quicksketch’s picture

Ha! I can't believe that worked ;)

What's happening is the empty array of values is still left, even though all the values are gone. This *should* remove the empty array too.

  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
  // unset($form_values['submitted_tree']['element_key']);
  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]);
          if (empty($form_values[$key])) {
            unset($form_values[$key]);
          }
        }
      }
    }
  }

  webform_empty_fields($form_values['submitted_tree']);

  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);
vistree’s picture

Hi, you are amazing!!! Works perfect without any modifications.

Thank you very much!!!!!!!!
No, everything is perfekt!!

Regards

vistree

quicksketch’s picture

Status: Active » Closed (fixed)
ryndog’s picture

Hey there,
I am trying to accomplish this as well (empty fields to be excluded in the form email). After editing my drupal/modules/webforms/webform-mail.tpl.php I am still getting empty fields sent in my emails. Here is what I have done:

Edited the drupal/modules/webforms/webform-mail.tpl.php file to read:

<?php
// $Id: webform-mail.tpl.php,v 1.1.2.5 2009/11/06 00:59:47 quicksketch Exp $

/**
 * @file
 * Customize the e-mails sent by Webform after successful submission.
 *
 * This file may be renamed "webform-mail-[nid].tpl.php" to target a
 * specific webform e-mail on your site. Or you can leave it
 * "webform-mail.tpl.php" to affect all webform e-mails on your site.
 *
 * Available variables:
 * - $form_values: The values submitted by the user.
 * - $node: The node object for this webform.
 * - $user: The current user submitting the form.
 * - $ip_address: The IP address of the user submitting the form.
 * - $sid: The unique submission ID of this submission.
 * - $cid: The component for which this e-mail is being sent.
 *
 * The $cid can be used to send different e-mails to different users, such as
 * generating a reciept-type e-mail to send to the user that filled out the
 * form. Each form element in a webform is assigned a CID, by doing special
 * logic on CIDs you can customize various e-mails.
 */
?>
<?php print t('Submitted on @date', array('@date' => format_date(time(), 'small'))) ?>

<?php if ($user->uid): ?>
<?php print t('Submitted by user: @username [@ip_address]', array('@username' => $user->name, '@ip_address' => $ip_address)) ?>
<?php else: ?>
<?php print t('Submitted by anonymous user: [@ip_address]', array('@ip_address' => $ip_address)) ?>
<?php endif; ?>


<?php print t('Submitted values are') ?>:

<?php
  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
  // unset($form_values['submitted_tree']['element_key']);
  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]);
          if (empty($form_values[$key])) {
            unset($form_values[$key]);
          }
        }
      }
    }
  }

  webform_empty_fields($form_values['submitted_tree']);

  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);

?>

<?php print t('The results of this submission may be viewed at:') ?>

<?php print url('node/'. $node->nid .'/submission/'. $sid, array('absolute' => TRUE)) ?>

Let me know if I can provide any more information. Any help would be greatly appreviated. Thanks in advance!

ryndog’s picture

Any idea?

ahimsauzi’s picture

Subscribing

CubicX’s picture

Version: 6.x-2.3 » 6.x-3.0-beta5

Any help on how to do this in 6.x-3.0-beta 5?. Above methods won't work sadly enough.

breethink’s picture

Subscribing

iwant2fly’s picture

Subscribing - I get the following error when using the previously mentioned code

warning: Invalid argument supplied for foreach() in modules/webform/templates/webform-mail.tpl.php on line 29.

hiweed’s picture

Subscribing.
Just like in email, I also want to the webform displays only the non empty fields in the result (Submissions) page.
Thank you!

Anonymous’s picture

Subscribing

slieps’s picture

I also want to remove the empty fields from the e-mail? I have a very long form with many fieldsets.
All the solutions above won't work.
Is there a solution what works with the webform module 6.x-3.4.

Thank you

jwinton’s picture

Version: 6.x-3.0-beta5 » 6.x-3.6

Subscribing

askibinski’s picture

The solution above is for the 6.x-2.x branch.

The solution for the 6.x-3.x branch can be found here:
http://drupal.org/node/749360#comment-2751838

rajmataj’s picture

Version: 6.x-3.6 » 6.x-3.20
Issue summary: View changes
Status: Closed (fixed) » Active

Can anyone suggest a working solution for Webform 6.x-3.20? Have tried the above solution in template.php:

<?php
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);
}
?>

...which does not seem to work and comment #23 refers to what has become the 7.x version.

Thanks.

liam morland’s picture

danchadwick’s picture

Status: Active » Closed (won't fix)

The 6.x branch is receiving critical bug fixes only.