Hi there,

I've got a template with various fields;

bla bla bla

    Name: %value[dea_details][dea_name]
    Surname: %value[dea_details][dea_surname] 
    Telephone: %value[dea_details][dea_telephone] 
    Email: %value[dea_details][assessors_email] 
    %value[dea_details][assessor_url]

------------------------------------------------------

- Home Owner & Building details -

  Contact Details

   Name: %value[contact][name]
   Telephone: %value[contact][landline]
   Email: %value[contact][email_address]

  Preferred Survey Date

    Day: %value[survey_date][if_yes_date][preferred_day]
    Time: %value[survey_date][if_yes_date][preferred_time]

  Property Location:

    Post Code: %value[property_address][post_code]

  Building Details:

    Type: %value[building_details][building_type]
    Construction Year: %value[building_details][property_construction_year]
    Rooms: %value[building_details][number_of_rooms]

    Conservatory: %value[building_details][conservatory_g][conservatory]

    Loft converted: %value[building_details][loft_rooms][loft_room]
    Conversion Year: %value[building_details][loft_rooms][if_yes_loft][loft_rooms_construction_years]

    Extension/s: %value[building_details][extensions][extensions]
    How many: %value[building_details][extensions][if_yes_ext][extensions_number]
    Construction Year/s: %value[building_details][extensions][if_yes_ext][extensions_construction_years]

    Other:  %value[other_details][notes]

Message was submitted %date from the IP address %ip_address
  
The results of this submission may be viewed at:
%submission_url 

Some of those fields aren't obliged to be filled and when they aren't filled I get this result in the email;

Building Details:

Type: Maisonette
Construction Year: 1984
Rooms: 2

Conservatory: Yes

Loft converted: No
Conversion Year: [loft_rooms][if_yes_loft][loft_rooms_construction_years]

Extension/s: No
How many: [extensions][if_yes_ext][extensions_number]
Construction Year/s: [extensions][if_yes_ext][extensions_construction_years]

Other:

It isn't an important error but doesn't look good.

Any idea what I'm doing wrong?

Thanks

Comments

quicksketch’s picture

Title: Some tokens are printes as is if leaved empty » Parts of nested tokens (in fieldsets) printed when left empty
Version: 6.x-3.9 » 6.x-3.14
Category: support » bug

This is a duplicate of #1191984: Webform token values not appearing in email text, but I've closed that issue in favor of this issue because this one is more succinct. This looks like it's a bug in Webform's token handling.

manoloka’s picture

Thanks for your quick answer.

If you need any more data or testing let me know what I have to do.

jamessmall’s picture

Hi.

I'm experiencing the exact same issue, and I'm happy to help with any testing required.

Any update on what might be causing it?

Thanks
James

quicksketch’s picture

Right now we haven't looked into it too much because the new token integration in the 7.x-4.x branch has solved this problem by switching to the Drupal core token system. Still happy to look at patches, but this isn't a high priority for myself.

charlie-s’s picture

The problem is in webform.module's _webform_filter_values() function. The str_replace() function strips away fieldsets.

<?php
  $find = array_keys($safe_replacements);
  $replace = array_values($safe_replacements);
  $string = str_replace($find, $replace, $string);
?>

So if %email['camp_weeks'] => fieldset and %email['camp_weeks']['camp_weeks'] => grid, str_replace() will strip out %email['camp_weeks'] from the original $string to be tokenized, and you'll be left with the useless ['camp_weeks']. The cleanup code won't see it since it's missing the token type and fieldset strings. Not sure how to combat at the moment.

gstout’s picture

csdco

The start of the problem is here. Only the first field set is getting iset to ''

    // Provide blanks for components in the webform but not in the submission.
    $missing_components = array_diff_key($node->webform['components'], $submission->data);
    foreach ($missing_components as $component) {
      $parents = webform_component_parent_keys($node, $component);
      $form_key = implode('][', $parents);
     $replacements['email'][$format]['%email[' . $form_key . ']'] = ''; 
     $replacements['email'][$format]['%value[' . $form_key . ']'] = ''; 
    }

I get this printed as a field's results, which is nested in field sets, on my form if there is no data entered for the field
[ethnic_racial_background_optional][racial_categories]
if I comment out the last two "$replacements" lines above I get
%email[background_information][ethnic_racial_background_optional][racial_categories]

So this part of the function is only setting %email[background_information] to blank and leaving the rest. I don't know how to fix it but this is the start of the issue. Hoping you're a better coder than I and can offer a solution.

gstout’s picture

I fixed it but forgive me if this is hacky. This worked for me.
The problem was the logic was treating field sets like things that should be getting user entered data. If they didn't get data, which they never would, then they it striped them out and broke other form components for which they were the parents,
So I just skip them.

    $missing_components = array_diff_key($node->webform['components'], $submission->data);
    foreach ($missing_components as $component) {
      if($component[type] != "fieldset"){
        $parents = webform_component_parent_keys($node, $component);
        $form_key = implode('][', $parents);
        $replacements['email'][$format]['%email[' . $form_key . ']'] = ''; 
        $replacements['email'][$format]['%value[' . $form_key . ']'] = ''; 
      }
    }
charlie-s’s picture

Thanks gstout, I will give that a shot and report back here.

charlie-s’s picture

StatusFileSize
new1.17 KB

It works. Patch supplied. But this is probably not the most ideal solution since the real problem is, I think, the fact that we have brackets touching each other in the fieldset-nested tokens. No simple way of combating this pops out at me.

I'm fine deploying this right now on a production site since it solves the problem, but maybe something more long term would be to check for any field type that a token doesn't make sense for, as opposed to just fieldsets? Something like:

<?php
if (!in_array($component['type'], array('fieldset', 'container', 'what-else-is-there?')) {
  ...
}
?>
FinderFees’s picture

Thank you very much for this change. I made it to my webform module as well and it does exactly what I want. :)

rootwork’s picture

Version: 6.x-3.14 » 7.x-3.19
Component: Miscellaneous » Code
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.54 KB

Attaching a patch that applies to 7.x-3.19.

quicksketch’s picture

Status: Needs review » Needs work

Rather than hard-coding "fieldset" into the module, it would be better if we checked for the "group" property on the type, as other types of components (i.e. pages) can also be empty.

So instead of:

if ($component['type'] != "fieldset") {

Use:

if (!webform_component_feature($component['type'], 'group')) {

Or maybe this for better readability:

if (webform_component_feature($component['type'], 'group') == FALSE) {
quicksketch’s picture

Hm, and we need a patch for the 4.x version of the module, as the _webform_filter_values() function basically does not exist any more.

rootwork’s picture

Yeah, I figured it would need to be updated for 4, I just wanted to share my adaptation for 3. #12 also makes sense.

FreeAndEasy’s picture

Patch #12 works for current 3.20 also.
Please commit.

shawn dearmond’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB

Here is a re-rolled patch against 7.x-3.x, including @quicksketch's recommendations from #12.

I did 3.x because that's what I'm using when noticing the issue. If I have time, I'll see about rolling a 7.x-4.x patch. In the mean time, I'd love to see this applied to 7.x-3.x.

shawn dearmond’s picture

Version: 7.x-3.19 » 7.x-3.x-dev

Testing at 7.x-4.x, this does not seem to be an issue. The core Token system seems to take care of it.

Moving it to 7.x-3.x-dev.

danchadwick’s picture

Status: Needs review » Closed (won't fix)

Version 7.x-3.x is receiving critical bug fixes only.

torotil’s picture

Assigned: Unassigned » torotil
Status: Closed (won't fix) » Reviewed & tested by the community

That one's critical to me. ;)

@DanChadwick: I think I'm finally stepping up to take maintainership of the 7.x-3.x if no-one else does. At least as long as I am still using webform-7.x-3.x in production sites. And that will be a while.

The last submitted patch, 9: 1332820.patch, failed testing.

The last submitted patch, 11: webform-fieldset_tokens-1332820-11.patch, failed testing.

torotil’s picture

Status: Reviewed & tested by the community » Needs work

It seems that this isn't enough after all. For HTML-emails fieldset-components render as an empty fieldset (two div-elements). Their tokens are already populated in the first loop in _webform_filter_values(). In the second loop (that #16) the token values is then replaced with an empty string. I think we have to find a better way to deal with this.

torotil’s picture

Status: Needs work » Needs review
StatusFileSize
new3.37 KB

Here is a more comprehensive patch using a different approach:

  1. It still includes an empty string for fieldsets as token. This avoids having tokens suddenly appear on sites that "rely" on tokens of fieldsets being replaced.
  2. It guarantees a post-order of tokens (nested token always is replaced before it's parent). This is achieved by only looping on $node->webform['components'] (which is guaranteed to be a pre-order traversal) and then reversing the array.

Status: Needs review » Needs work

The last submitted patch, 23: webform-fieldset_tokens-1332820-23.patch, failed testing.

torotil’s picture

Status: Needs work » Needs review
StatusFileSize
new3.42 KB

Ah that was actually the interdiff. Attaching a patch that is really based on 7.x-3.x.

danchadwick’s picture

Category: Bug report » Task

Changing to task to reflect the maintenance status of this branch.

  • torotil committed 4e42c58 on 7.x-3.x
    Issue #1332820 by torotil, csdco, quicksketch, gstout, manoloka: Parts...
torotil’s picture

Category: Task » Bug report
Status: Needs review » Fixed

I've tested the patch some more and it still looks good. It's now committed to 7.x-3.x.

@Dan: I think this is still a bug - otherwise I wouldn't have the mandate to fix it since 7.x-3.x is "bugfixes only" - isn't it?

danchadwick’s picture

Re #28 -- No problem changing back to bug report after its fixed. What I'm trying to avoid is having bugs in the issue queue for a branch for which we have no commitment to fix bugs, other than critical bugs. The same goes for the 8.x branch, for which we have no expectation of being bug-free and the "bugs" are really just things that haven't been ported yet.

The purpose of this is so that the number of bugs accurately reflects the quality of the fully-supported branches, which right now is only 7.x-4.x.

Status: Fixed » Closed (fixed)

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