1

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 :-(

4
  • 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. Commented Jul 8, 2014 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... :-( Commented Jul 8, 2014 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 Commented Jul 8, 2014 at 9:05
  • I just found this article - covers special cases better than comment you linked. Commented Jul 8, 2014 at 9:15

1 Answer 1

4

In hook_form_alter(), add a new submission handler before the other submission handlers.

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

function pwe_webform_form_submit($form, &$form_state) {
  $form_state['input']['submitted']['last_name'] = ucfirst($form_state['input']['submitted']['last_name']);
}
1
  • Many thanks! The origin of the template value is $form_state['values']['submitted']['last_name'] in validation hook! Commented Jul 8, 2014 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.