I'm trying to set the selected value of a webform select form element before it is rendered. I'm using the below code and the textfields are working just fine but no luck with the select options.

function mytheme_preprocess_webform_form(&$vars) {
    $vars['form']['submitted']['emailAddress']['#value'] = '[email protected]'; //This is working
    $vars['form']['submitted']['country']['#selected'] = 'BARBADOS'; // This is not working even though #default value is used instead

I can't figure it out why this is not working for select field. I would be glad if someone could help me.

Thanks in advance.

2 Answers 2


You can set value by using #value

$vars['form']['submitted']['country']['#value'] = 'BARBADOS';

But make sure that the select option key should be same


<option value="BARBADOS">BARBADOS</option>
$vars['form']['submitted']['country']['#value'] = 'BARBADOS'; //will work

<option value="barbados">BARBADOS</option>
$vars['form']['submitted']['country']['#value'] = 'BARBADOS'; //will not work
$vars['form']['submitted']['country']['#value'] = 'barbados'; //will work
  • Thanks man, I've spent hours for this, didn't expect it would be that easy. Aug 11, 2017 at 11:08
  • Always welcome :) Aug 11, 2017 at 11:09

For webform forms, I think you have to set #value to TRUE on the option itself.

Something like this should work:

$vars['form']['submitted']['country']['BARBADOS']['#value'] = TRUE;

If there are any other default values, you'll have to set those to FALSE.

  • Thanks a lot but nope, didn't work either Aug 9, 2017 at 7:06

Your Answer

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

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