Posted by SophieG on March 22, 2010 at 1:26pm
32 followers
| Project: | Webform |
| Version: | 7.x-3.17 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Issue Summary
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 :
<?php
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
Comments
#1
You can do this by theming the theme_webform_element_text() function in your template.php file.
<?phpfunction 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);
}
?>
#2
Thanks !! it works perfectly ! But just for one webform.... Any idea why ?
#3
That should affect all Webforms on your site, since all Webform e-mails go through that function.
#4
Marking fixed, please reopen if you have another question about this issue.
#5
Automatically closed -- issue fixed for 2 weeks with no activity.
#6
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!
#7
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!!!
#8
#9
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.
#10
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!
#11
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) {#12
@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.
#13
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);
}
#14
@jpsimon
Do not prefix _webform_element_text with your theme name; use theme_webform_element_text.
You should end up with:
<?php
function YOURTHEME_webform_element_text($element, $value) {
if (strlen(trim($value)) == 0) {
return '';
}
return theme_webform_element_text($element, $value);
}
?>
#15
thank you so much, that worked like a charm!!
#16
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
<?phpand?>at the end of the code? At the beginning of the template.php, it already has the<?phpbut 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
#17
@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.
#18
Here's #1 updated for D7
<?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);
}
?>
#19
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.
#20
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?
#21
@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).
#22
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.
#23
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?
#24
I would second that... since it would make it easier for me to get this to work... :)
#25
@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.
#26
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...
#27
@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.
#28
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?
#29
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.
#30
patch in #29 works great -- only thing, only seems to work with textfields / textareas, not with selects (haven't tried other component types).
#31
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.
#32
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
<?phpfor($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.
#33
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)
#34
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
#35
Here's a solution that can go in a webform-mail-nid.tpl.php file:
<?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
";
?>
#36
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]);
}
}
}
?>
#37
#35 - this worked for me, modifying the if value = 0 line
<?phpif ($value['value'][0] == '0' || empty($value['value'][0])) {
?>
#38
Thanks @vernond for the patch! I'll review this when I get the chance. Looks excellent at first blush. :)
#39
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
#40
The last submitted patch, webform-7-emailnonempty-749360-22.patch, failed testing.
#41
#42
Well, this issue moved by accident to the wrong issue queue, bakc to webform :)
#43
Thanks @Hanno.
#44
Hi Sheldon
<?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
";
?>
Can you pleas help me in customizing the titles(field title) with html tags and a line break between the fields.
#45
@Akshita Can you explain what you need? I need more detail.
#46
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
#47
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.
#48
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:
<?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
";
?>
#49
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
#50
@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.
#51
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.
#52
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?
#53
Sheldon's #35 with dalberts69's #37 accomplished the email results I need, but leaves me with errors.
at webform/emails
24. foreach ($submission->data as $cid => $value) {and at webform/emails edit
41. $email_values = webform_submission_render($node, $submission, $email, 'text');Quicksketch's #1 is working great without any errors.
Thanks.
#54
akalata's #18 is working great for me (so far) with Drupal 7.15 and Webform 7.x-4.0-alpha6.
Thanks
#55
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
#56
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.
#57
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.
#58
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);
}
#59
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
#60
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
#61
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?
#62
#63
Thanks Akshita #58 that worked!
gr8britton I also restarted apache and it worked for me. You can try that.
#64
I'll give that a shot. Thank you.