In order to help sort through email enquiries submitted via a webform, I would like to be able to include several of the user submitted values in the subject line. I see that you can include profile fields, and server variables, but i really need to include the values submitted by the user, such as the city, and date of an event.

eg. for the subject to read:

"Event enquiry from [name] for [eventdate] in [city]"

I can imagine that this is a feature that would be of value to many people.

CommentFileSizeAuthor
#11 webform_tokens_parse_subject.patch1.22 KBdomesticat

Comments

quicksketch’s picture

Marked http://drupal.org/node/162344 as duplicate

budda’s picture

Version: » 5.x-2.x-dev

Is this a candidate for use of the token.module ?

I'd also like to see such a facility.

jamiefifield’s picture

Version: 5.x-2.x-dev » 6.x-2.0-beta6

This is exactly what I'm trying to figure out how to do (without editing templates and php code if I don't have to). Using 6.x-2.0-beta6.

domesticat’s picture

Subscribing.

CyberThijs’s picture

I would also like to see this feature come to webform! Subscribing

quicksketch’s picture

Title: referring to user submitted field values in subject » Referring to user submitted values in subject
quicksketch’s picture

The subject field already is automatically checked for tokens, however we'd need a set of tokens that contain the submitted values of the form as recommended in #300075: Introducing %value (previously %submitted) token to load submitted values.

domesticat’s picture

Out of curiosity, anything we could do to help this along? I'm starting to get more questions from my users as their usage of webforms increases.

quicksketch’s picture

domesticat, sure providing patches for this functionality is welcome. That issue I mentioned in #7 is more descriptive of how the solution should be implemented.

domesticat’s picture

I've still got to create the patch, but I dug through webform.module this morning and I think I see how to do it.

In version 6.x-2.3, edit webform.module as follows:

$email_subject = $node->webform['email_subject'];

becomes

$email_subject = _webform_filter_values($node->webform['email_subject'], $node, $form_state['values']['submitted']);

and then modifying a chunk of code starting at line 2024.

  // Submission replacements.
  if (isset($submission)) {
    foreach ($submission as $cid => $value) {
      $find[] = '%cid['. $cid .']';
      $replace[] = $value;
    }
  }

  // Load the user profile.

becomes

  // Submission replacements.
  if (isset($submission)) {
    foreach ($submission as $cid => $value) {
      $find[] = '%cid['. $cid .']';
      $replace[] = $value;
    }

    foreach ($_POST['submitted'] as $key => $variable) {
      $find[] = '%webform['. $key .']';
      $replace[] = $variable;
    }
  }

  // Load the user profile.

That last chunk isn't even strictly necessary, it seems; once the value of $email_subject is passed through _webform_filter_values(), there was existing code to do replacements based on %cid, but it just seemed a little ... non-intuitive to do it that way? I thought maybe supplying the option of using %webform[$fieldname] would make a lot more sense to non-programmers who didn't want to dig for the %cid for each field.

Is that really all that's required? Seems oddly simple. Maybe I'm forgetting something. Thoughts?

domesticat’s picture

Status: Active » Needs review
StatusFileSize
new1.22 KB

A patch for the code idea in #10 is attached.

Edit: It looks like it works for referring to items by cid, as in %cid[6], but if the name of the field has a space in it, the %webform[] idea doesn't work right. Bah. Well, it's a start anyway.

quicksketch’s picture

Status: Needs review » Needs work

This also isn't going to work with fieldsets or multi-page forms. We should be checking $form_state['values']['submitted'] for these values, not $_POST.

domesticat’s picture

Yeah, I knew it was a nasty, quick kludge. Beat nothing, though. I always forget the multi-page forms part since I'm not using them. Hopefully I can re-examine it midweek.

wildebeest’s picture

Subscribing.

domesticat’s picture

@quicksketch - I've come back to this patch request periodically and have tried to work out an elegant way of doing it, but every time I kept getting tangled up. I'm posting this in the hopes of triggering ideas in someone more capable than myself.

My first thought was to add in additional code to _webform_filter_values() but I realized it only had access to $form_state['values']['submitted']. That gave me only numeric keys, and not the kind of friendly variable-name keys I know I'd prefer. I looked at calling more of the $form_state variable and realized it seemed a bit redundant to provide it two separate times in arguments. I then went digging and rapidly realized the number of times I'd have to edit calls to this function across the entire module was higher than I expected -- and I didn't fully understand the implications of doing so, either. It bothers me to admit that I don't have time to figure out all of the pieces, but that's my reality right now.

I started weighing the need to touch all of those separate parts of the code versus writing a single extra function that would work on just this single request. I'm posting this idea knowing full well that this isn't a particularly good way of getting this feature, but it's the best I have time to provide and I'm hoping someone else will pick it up and run with it.

The first replacement is a two-step one, kept from before:

$email_subject = $node->webform['email_subject'];
becomes

$email_subject = _webform_filter_values($node->webform['email_subject'], $node, $form_state['values']['submitted']);
$email_subject = _webform_filter_subject($email_subject, $form_state['values']['submitted_tree']);

Then this new function is added after the end of _webform_filter_values():

 /**
 * Filters only the subject line for strings of the format %webform['foo']
 */
function _webform_filter_subject($subject, $submitted_values, $strict = TRUE) {

  foreach ($submitted_values AS $key => $value) {
    // Multi-selects can potentially provide responses that are arrays. Flatten them before using them.
    if (is_array($value)) {
      $value = implode(', ', $value);
    }
    $subject = str_replace("%webform['" . $key . "']", $value, $subject);
  }

  return $strict ? filter_xss($subject) : $subject;
} 

The first pass does the same level of substitution performed on the email body; the second gets me the tasty extra stuff. Using $form_state['values']['submitted_tree'] gave me a short and easy way to use strings like %webform['location'] in the subject line, which felt reasonably polished to me.

I'm pretty sure this code is not the right way to go, so please understand I'm posting it with no expectation of seeing it committed. I recognize I can't give the module the time needed to rewrite all the little tendrils that affect _webform_filter_values() but this, at least, solves the problem for me.

Thoughts?

salmosri’s picture

i've been playing around with this solution, i have used standard forms with nothing too complex, and it works a charm, and i would say very elegantly. i can't say i have stress tested it, but for simple implementations it works a treat :).

Cheers,
Shadi

marcvangend’s picture

subscribe

tommyk’s picture

Thanks for the work on this. I'll be glad when it makes it into the module.

jeffschuler’s picture

aether’s picture

It would appear that the seeds of this functionality are in the 6.x branch but appear to be undocumented (and likely unsupported) at the moment. I was able to create a custom email subject using user submitted values the following way:

1. Add a new form component of the type "hidden". Let's call it "Email subject".

2. Using the desired email subject in the original post for this issue as an example, in the default value field for this component enter:

Event enquiry from %cid[x] for %cid[y] in %cid[z]

The x, y, and z keys above should represent the component id numbers (cid) relating to the form's component fields for "name", "eventdate", and "city" (Hint: you can find the cid for a form component by mousing over it's Edit link in the table on the "Form components" page and noting the last argument in the link's url). It seems that %cid is set up in the module as a token for the array of the form's submitted values.

3. Choose "Email subject" in the Component drop-down for the E-mail subject setting on the webform's Configuration page.

Naturally, this same technique will work for the "Email from name" and "Email from address" settings as well.

sinigur’s picture

I just tested out #20 and that worked. Good enough for me.

john bickar’s picture

Confirming that the method in #20 works for me as well in 6.x-2.7.

Great usability improvement - thanks!

tommyk’s picture

The method in comment #20 will not work if the component is a select field with multiple values set as the option. The word "Array" is returned instead of any of the text of any of the options selected.

Does anyone know of a workaround?

I'm trying to make the subject of the emails contain the text of one or more of the options selected. It's not that big of a deal, but it'd be nice.

Marcus Aurelius’s picture

I have the same problem, I want a reservation date to be shown on the subject line. The %cid[] gives me "Array". I have found the field "no" in the table webform_submitted_data to point to a part of a field. So I tried almost every combination of %cid[] and %no[], but nothing works. I guess a patch will be necessary for this. Any progress on that?

Marcus Aurelius’s picture

I have the same problem, I want a reservation date to be shown on the subject line. The %cid[] gives me "Array". I have found the field "no" in the table webform_submitted_data to point to a part of a field. So I tried almost every combination of %cid[] and %no[], but nothing works. I guess a patch will be necessary for this. Any progress on that?

elijah lynn’s picture

#20 was very helpful.

For the noobs like me who are wondering if %cid[x] means %cid8 or %cid[8], I will save you a step, it is the latter!

%cid[8]

That would be correct. Not sure what these guys are talking about for arrays though.

quicksketch’s picture

Status: Needs work » Closed (duplicate)
prophet108’s picture

Aether. Thanks for this solution!

pieterbezuijen’s picture

Aether: Many thanks for this solution!

clintcar’s picture

#20 does work, but for (select) you do get the ARRAY error in the subject line. I was able to change my settings to "List box" and cleared this up as a work around. Wouldn't help if i wanted users to select more than one option, but it might get around this error for some until fixed. Thanks Aether!

jeffschuler’s picture

A note for those using Webform 3:
Using %cid[x] no longer worked when I upgraded to Webform 6.x-3.0-beta5 (from 6.x-2.x.)
However, using %value[fieldkey] -- as described in #300075: Introducing %value (previously %submitted) token to load submitted values -- did work.

sense-design’s picture

A comment / description should be added below the subject field, like it is within the "E-mail template" fieldset.

machete1’s picture

Per Post #31 -

I have a text field in my webform with Field Key project. This field is also the first component I created for the form so it's cid is 1.

In the Custom Subject field for email submission, I tried using %value[project] and %value[1]. In both instances, I received the %value[project] (or [1]) in the email subjects received.

Can someone explain what I am doing wrong?

jeffschuler’s picture

@machete1: what version of webform are you using?
Using %value[key] is working fine for me with 6.x-3.9.

machete1’s picture

@ jeffschuler:

I am using 6.x-3.9 as well.

To confirm a couple things:
-- You actually type the brackets [ ] correct?
-- Where you have the word key in your example is that a number (cid) or the Field Key name?

I just want to make sure that I am not having an oblivious moment of stupidity.

Thanks Jeff!
Eddie

ejohnson’s picture

Any suggestions on what I'm doing wrong? Running Webform 6.x-3.14

In the E-mail subject - Custom field, I've tried:

Customer Number: %cid[19]
Customer Number: %cid[customer_number]
Customer Number: %value[19]
Customer Number: %value[customer_number]

19 being the component ID when hovering over the Edit link
customer_number being the Field Key

Can anyone be more clear on what to enter?

Thanks.

EDIT: Due to the fact that it was a nested field with a fieldset, I had to use:

%value[service_address][customer_number]

joran lafleuriel’s picture

thanks ejohnson

Your solution in #37 saved me !
Tokens for values in fieldset must mention the fieldset