0

I summon your wisdom on this day because I can't figure out how to alter submitted Webform values that are used in email templates.

Here's my situation:

I have a "contact us" form (set with Webform API) with first name, last name, email and message fields. Nothing spectacular. The submitted values are sent by email to the administrator.

In this webform setting > Email subject, I have this template:

[NAMEOFMYSITE | question from %value[first_name] %value[last_name]

Everything works fine. But my problem appears when the user forgets to put an uppercase in last name or first name value. It doesn't look professional in the email subject. And I want to force these two values with an uppercase as first letter to get something like:

NAMEOFMYSITE | question from John Doe

And never:

NAMEOFMYSITE | question from john doe

I tried to alter submitted values because I thought template's %value were based on it:

function pwe_webform_form_alter(&$form, &$form_state, $form_id) {
  [...]
  $form['#submit'][] = 'contactus_webform_submit'; // callback function on submit
}

function contactus_webform_submit($form, &$form_state) {
  $form_state['values']['submitted']['last_name'] = ucfirst($form_state['values']['submitted']['last_name']);
  $form_state['values']['submitted']['first_name'] = ucfirst($form_state['values']['submitted']['first_name']);
}

But even if I succeed to modify them, the %value in email doesn't change.

Of course, I dpm $form_state tree to see where the submitted values appear in it and where the template's %value could find its origin. It appears on several levels:

  • $form_state['input']['submitted']['last_name']
  • $form_state['values']['submitted'][2]
  • $form_state['values']['submitted_tree']['last_name']
  • $form_state['complete form']['submitted']['last_name']['#value']
  • $form_state['webform']['component_tree']['children'][2]['value'] (but always empty)

I altered every variable but the email value still the same. I'm about to ask myself if it is actually based on one of those variables... If not, on what is it based? And how can I alter it?

Please excuse the looooong post but I wanted it to be very clear. I hope you will help anyway :-(

  • I'm not sure if forcing uppercase is a good idea. Think of Jeanne d'Arc - spelling as Jeanne D'Arc would be an error. Don't know how probable it is in your demography, but usually I would say it is more professional to leave a mistake your user did, than to introduce a spelling error to his content. – Mołot Jul 8 '14 at 8:46
  • 1
    I totally agree with you @Mołot but I'm just the developer in that story, and I have to do some magic to execute requests from the company that I work for... :-( – Djouuuuh Jul 8 '14 at 8:49
  • I also found a very interesting comment in PHP manual about managing special string like Jeanne D'Arc: php.net/manual/en/function.ucfirst.php#81258 – Djouuuuh Jul 8 '14 at 9:05
  • I just found this article - covers special cases better than comment you linked. – Mołot Jul 8 '14 at 9:15
3

In hook_form_alter, add a new submit function before the other submit handlers, like so:

pwe_webform_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
  case 'YOUR_WEBFORM':
       array_unshift($form['#submit'],'pwe_webform_form_submit');
       break;
  } 
}

And then write your submit handler, something like this:

function pwe_webform_form_submit($form, &$form_state){
  $form_state['input']['submitted']['last_name'] = ucfirst($form_state['input']['submitted']['last_name']);
}

You can intercept the submit handler with dpm($form_state['values']) calls, to make sure you're using the right array keys, etc.

  • Many thanks! The origin of the template value is $form_state['values']['submitted']['last_name'] in validation hook! – Djouuuuh Jul 8 '14 at 9:03
  • My pleasure. :) I agree with @mołot, though, that it might lead to trouble in some cases. :) – Ollie Jul 8 '14 at 9:11
  • 1
    It's a problem for my future me xD – Djouuuuh Jul 8 '14 at 9:15
  • 1
    @Ollie I find it amusing how you miss-capitalized my nickname under this very question :D – Mołot Jul 8 '14 at 9:19
  • 1
    haha! I was struggling with your ł – Ollie Jul 8 '14 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.