Hi, I can't seem to get the default value for %profile[field_first_name] to work. I add the field via Administration » Configuration » Account settings » Manage Fields. Nothing shows up as the default value when viewing the form. Any suggestions?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Title: %profile[key] default value working? » Add support for user Field module fields
Category: support » feature

These fields do not work because they are not actually supported (yet). Those fields are not Profile fields, they are Field fields (confusing I know). Webform currently only supports the fields added by Profile module (which I wouldn't really recommend over the Field module fields that you're already using). I'm moving this to a feature request because it would make sense to support such fields.

Note that this problem would be solved a separate way through #1001798: Rewrite token replacement system to use D7 tokens.

skhot’s picture

Makes since. Thanks for the update.

quicksketch’s picture

Title: Add support for user Field module fields » Add support for user Field module fields e.g. %profile[field_name]
Status: Needs review » Active
cartagena’s picture

Thank you.

chaloum’s picture

Quicksketch

Just trying to understand your post(#1) are you saying we should not use the profile module, which is think is Profile2 but instead use the core fields method as described by skhot?

thanks

quicksketch’s picture

@chaloum: The "Profile" module is still shipped with Drupal 7 but it is impossible to enable because its use is deprecated. The only way to use Profile module in Drupal 7 is to have the module enabled when you upgrade from Drupal 6 (or manually enable it in the database).

Profile module would put things directly in the $user object like this:

$user->profile_first_name = "John";

Field module on the other hand puts them in the $user object like this:

$user->field_first_name[0][$language] = "John";

Because our tokens do not support nesting (see #1037524: Support array-format tokens in [current-page:query:x] tokens), you can't use a token like %profile[field_first_name][0][und] (which is what this token would be usually). It'd be nice to make these tokens significantly simpler, or to implement #1001798: Rewrite token replacement system to use D7 tokens which would make tokens more complicated but at least consistent with the rest of Drupal.

miro_dietiker’s picture

Subscribing

Nil Gravitas’s picture

Subscribing! I really want this to happen.

bjaxelsen’s picture

I've implemented this in a utility module. It might be ported into the webform module:

/**
 * Implements hook_form_alter().
 * Pre-fill fields in calendar signup (curently this is only included in Webform module for deprecated profil fields)
 */
function cisbo_utility_form_alter(&$form, &$form_state, $form_id) {
  if (substr($form_id, 0, 19) == 'webform_client_form') {
    global $user;
    $account = user_load($user->uid);
    foreach ($form['submitted'] as &$field) {
      if (isset($field['#default_value']) && ($field['#default_value'] === '') && (!empty($field['#webform_component']['value']))) {
        // Do regular expression to extract information about substitutions
        preg_match_all('%profile\[(field_.*?)\]%', $field['#webform_component']['value'], $extract, PREG_PATTERN_ORDER);
        if (isset($extract[1][0])) {
          $field['#default_value'] = $account->{$extract[1][0]}['und'][0]['value'];
        }
      }
    }
    dpm($form);
  }
}
Theo’s picture

Thanks bjaxelsen! That code worked great.

mariagwyn’s picture

Would really like this as well, happy to test. to be clear though, I do not have any profile fields (new site, not use) but only "Field fields" as quicksketch labelled them above.

Maria

Buliwyfa’s picture

The hook to support "Field fields" is really nice and +1 to be ported to webform realease.

Thanks ;D

micke793’s picture

I have the same problem as skhot has, did bjaxelsen code fix that problem?

If it does, how do I implement that code?

//Micke

shotokai’s picture

FileSize
2.82 KB

@micke793 - the code does fix the issue and let you pull 'field fields' from the user entity into your webform.

To use it you can follow these steps if you want to learn the basics of module development: http://drupal.org/node/1074360 or you can use the attached. Worked for me. Just install like any other module - although you might want to stick it in a 'custom' directory so that you know this a not a proper contributed module in the future...

@bjaxelsen - thanks for the code. final piece in a puzzle for me..

e-weavers’s picture

Category: feature » bug

Hi,
I am getting this error on the form page:

Notice: Undefined index: und in webform_userfields_form_alter() (line 21 of C:\wamp\www\Drupal\sites\all\modules\webform_userfields\webform_userfields.module).

Line 21: $field['#default_value'] = $account->{$extract[1][0]}['und'][0]['value'];

Please Help!

shadowdknight’s picture

Does this works with Profile2?
How do I include this in the submission email eg:%profile[field_user_ref] ?

Please help!
Thanks

TimelessDomain’s picture

Category: bug » feature
Status: Active » Needs review

#14 worked great for core profile fields (did not try profile2 yet)

actually. it does work but i am getting the same error as #15

TimelessDomain’s picture

Status: Needs review » Needs work
slbrassard’s picture

FileSize
1.32 KB

@shotokai: Thank you for the code. It was very helpful. I found that profile field tokens inside of fieldsets were not being handled so I modified your code a bit to handle that case. Posting it here in case it's helpful to someone else.

Polyspiral’s picture

Will the above code be out into the next release of Webform?

Thanks

quicksketch’s picture

Will the above code be out into the next release of Webform?

Not in its current state, since features need to be written as patches in order to be reviewed and included in the project.

chaloum’s picture

Desperately need this to become a reality as username tends to be meaningless to survey administrators, and having to get people the type in their name when its already in the system just generates complaints.

Is there a workaround outtside of the above code??

flightrisk’s picture

I've read through most of the forum and all the release notes for webform and still have no idea:

1. What is the current state of getting at all the tokens from the tokens module (in particular the user and current user tokens)
2. I have the version from July. Will I have to upgrade? What is the upgrade process?

guenneguez_t’s picture

FileSize
1.18 KB

Remplace the version of slbrassard if user field is not set.

Thanks
Thomas

flightrisk’s picture

Is this going to be a patch and then included in a release? Can anyone submit some official patch so we can get this inside the module and not have to have yet another module?

shotokai’s picture

This is the first patch I've submitted - so I've tried to follow the rules, but I'm sure I've missed something...
The patch is against 7.x-3.9-hotfix

shotokai’s picture

Status: Needs work » Needs review
Herr Lehmann’s picture

Patch from #26 works for me (sofar) - thanks!

flightrisk’s picture

Hi shotokai, can you provide a little documentation? After patching, how do I input the user fields? What is the format, or will they now display in a list for me to select?

stBorchert’s picture

Version: 7.x-3.4-beta1 » 7.x-3.x-dev
Status: Needs review » Needs work

Code review (without functional test):

+++ b/webform.module
@@ -1241,6 +1241,30 @@ function webform_form_alter(&$form, $form_state, $form_id) {
+    webform_userfields_replace($form['submitted'],$account);

Code style: missing space between $form['submitted'], and $account.

+++ b/webform.module
@@ -1241,6 +1241,30 @@ function webform_form_alter(&$form, $form_state, $form_id) {
+function webform_userfields_replace(&$fields, &$account) {

No need to pass $account as explicit reference as you hopefully never try to modify the object here ... ;)

+++ b/webform.module
@@ -1241,6 +1241,30 @@ function webform_form_alter(&$form, $form_state, $form_id) {
+      webform_userfields_replace($field,$account);

Code style: missing space between $field and $account.

+++ b/webform.module
@@ -1241,6 +1241,30 @@ function webform_form_alter(&$form, $form_state, $form_id) {
+        $field['#default_value'] = $account->{$extract[1][0]}['und'][0]['value'];

Please do *not* use ['und'][0] for fields. Never.
Use field_get_items() instead.

Btw.: patches are made against HEAD (7.x-3.x-dev).
2nd btw.: thanks for helping with this issue!

hpz’s picture

FileSize
2 KB

Here is my version of the webform_userfields module, which also replaces multiple occurences of a '%profile[key]' token whithin one webform default value.

hpz’s picture

FileSize
1.98 KB

Had to fix it. This one is better.

quicksketch’s picture

Status: Needs work » Fixed

Use the Webform 7.x-4.0-alpha1 release available on the project page, which includes #1001798: Rewrite token replacement system to use D7 tokens. Other than that it is identical to the current 3.x branch.

xcafebabe’s picture

Thanks for this improvement!

Status: Fixed » Closed (fixed)

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

rolandu’s picture

This change has now been in the 4.0-alpha-branch for almost a year, could this be pushed to the 3.x-branch? I believe a lot of people need it and have to use the alpha version because of this. (At least several of my sites are doing that.)

kuydigital’s picture

My site has a lot of forms and I'm afraid to remove the module and install 4.0-alpha-branch. Is it still possible to push this to the 3.x-branch as a recommended update in Drupal 7?

Thanks!