I already posted this on the forums but I figured this is probably the better place to ask this.

If one has a webform that is open to anonymous users, which asks information that is known for registered users (age, gender, ...), is it possible to remove this question from the display and/or fill in the value automaticallty, if the user filled the corresponding field in their profile?

Is this possible at all? How would you recommend me to do this?

Comments

quicksketch’s picture

You can do this by using the 4.x branch, which includes the typical Drupal token support so you could set the default value of fields to something like [user:field_first_name], [user:field_age], etc. based on the fields you have set up on your site.

P3t3r’s picture

And is there any way to process these fields first, e.g. compute their age from their birthday?

quicksketch’s picture

You could create your own tokens with the Token API through hook_tokens() and hook_token_info(). Webform itself doesn't provide any special post-processing on default values.

P3t3r’s picture

Version: 7.x-3.18 » 7.x-4.0-alpha3

This works for text fields, but it seems that select lists do not allow a default value in 4.0 alpha3. Is this intentional, and if so, why? It seems perfectly reasonable to allow a default key on select fields... is this a bug, or an intentionally missing feature?

quicksketch’s picture

Select lists definitely allow default values. Do you have Options Element installed? It hides the default value field since it displays them as radio buttons/checkboxes. You have to click the "Manual" link below the options element to set a default value that is a token.

P3t3r’s picture

Indeed, the problem was indeed with Options Element.

  • Doing what you say didn't work: then I can enter a default field, but after submission it turns out that the default was not saved! Not sure if this is a small bug in either module, or just an incompatibility.
  • I uninstalled the Options Element module, and then the defaulting works correctly.

So there is still some small incompatibility. Well, not that Options Element is a necessary module anyway.

I'll see if I can get the tokens working and report back here when finished (currently there's still a problem).

P3t3r’s picture

Status: Active » Fixed

It works now. :)
If anyone want to try it, I will add here for future reference that profile fields do not appear automatically in $user, you have to execute

  $user_data = user_load($user->uid);

and then grab them from $user_data.

gopisundar’s picture

@P3t3r
Can you please tell me where to add this line?

P3t3r’s picture

@gopisundar: I recommend reading the basic tutorial on creating modules, and this tutorial on making new tokens. Then, after adding new fields to your profile, the natural thing to do would be (as in the token tutorial) to write in your modulename_tokens() function:

 global $user;
 do_your_replacement_with($user->my_new_field);

however, that should be

 global $user;
 $user_data = user_load($user->uid);
 do_your_replacement_with($user_data->my_new_field);

instead.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.