Is there an easy way to pre-fill a webform with current user data (as in profile) using the default value field? If so, could you please post an example?

Comments

ullgren’s picture

A example can be found in the way the e-mail component is defined.
So a way to do it would be to add another component such as City.

In webform_form add city to the $component_types array

 $component_types = array('textfield' => t('textfield'),
                           'textarea' => t('textarea'),
                           'radiobutton' => t('radiobutton'),
                           'checkbox' => t('checkbox'),
                           'combobox' => t('combobox'),
                           'email' => t('e-mail address'),
                           'city' => t('City'));

Then implement a rendering for the city component in _webform_create_widget
[?
// placed above the "case 'email':"
case 'city':
if($user->uid && $user->city && empty($value)) {
$value = $user->city;
}
$maxsize = 255;
if ($extra && is_numeric($extra)) {
$maxsize = $extra;
}
return html_form_textfield('', $name, $value, 50, $maxsize).
($err?html_br().$err:'');
break;
?]

Somthing like that would do it. Please observe that this code is untested and just "concept code".

Maybe there would be a way to automate this so all fields specified in profile will have a component type in webform. This would be a great concept.
Some thing like this would to it:
1. If module profile exists add all specified fields to the $component_types array.
2. In _webform_create_widget it there is a $user->$type variable present use it as default value if no value is specified.

If you implement this please send me a patch and I'll include it. Otherwise please add it as a feature request and I'll see if I get time to implement it.

ullgren’s picture

Category: support » feature

In the CVS version there is now a functionality to add %profile[field_name] variables to both the default value and the description.
I guess this fills your need.

Anonymous’s picture

woop_light’s picture

Version: » 7.x-3.9

Thanks! Something strange is happening, however. When I try to prefill one of the components with a custom registration field that I have created (e.g. Company) by putting it in the default value textbox (%profile[field_company]), the webform does not get prefilled -- instead it is blank. However, if I use timezone it works (%profile[timezone]) and the component gets prefilled with "America/New_York".

Evidently it's acknowledging the %profile prefix, otherwise it would prefill the component with the error text (for example if I misspelled it as %profil[field_company], I would see "%profil[field_company]" prefilled in the component).

Can anyone help with why it is acknowledging my attempt to pass the value from the registration, but not actually passing it? I am on a tight deadline, and this would be a lifesaver. Also I have tried it a ton of times with different variables, so I'm confident it's not a typo on my part.

Got it.

1. Install and enable the Profile2 module.
2. Update your files with this wonderful patch: http://drupal.org/files/issues/1001798-webform-tokens-latest.patch.

Then you can prefill/prepopulate your webform components with the registration values stored by Profile2 the same way that you would normally do it. As the patch describes:

1. Just edit or add a new component of a webform to display the mentioned "component edit form".
2. Inside the form, navigate to the default value field.
3. Right below this field you can see the collapsed fieldset "TOKEN VALUES". Open it and set the default value for the field by just clicking on the desired token.

Perhaps there are other ways to do it, but this did the trick for me. Hope it saves someone time and headache.

woop

Also I'm using v 7.x-3.11

shadowdknight’s picture

Hi Woop,
Does the patch works for 7.x-3.15 ?
I tried to use this : %profile[field_pro2_ref] as default value,
but it doesnt shows.

Thanks!

edux’s picture

The patch in #4 works but there's a potential issue because you call token_replace() without the "clear=TRUE" option.
So if token can't replace a string it returns the original one.

Replace line 84 of the patch with:

$string = token_replace($string, $token_data, array('clear' => TRUE));