In D6 we could configure a webform field to pull profile data by using a token like %profile[key] and assigning the user field to the form [details here]. How can we to something similar in D7, using the user fields instead of the profile fields?

Comments

nikan’s picture

No one? Why? Is it a dumb question or nobody knows the answer?

wferrell343’s picture

no this is not a dumb question I need to know how to do this too.

k.halterman’s picture

I am also working on figuring this out as well.... thought maybe %profile was changed to %user, but that didn't work either.

polis23’s picture

I encounter the same problem. I tried % user as well but without success.

yelmontaser’s picture

You must replace the foreach line in 3042 webform.module per this new foreach.

foreach ($variable as $key => $value) {
    if(is_array($value) == false) {
        $replacement = (is_string($value) || is_bool($value) || is_numeric($value)) ? $value : '';
    } else {
        if(isset($value['year'])) {
            $replacement = webform_strtodate(webform_date_format(), $value['month'] . '/' . $value['day'] . '/' . $value['year'], 'UTC');
        }
        
        if(strpos($key, 'field_') !== false) {
            $replacement = $value['und'][0]['value'];
        }
    }

    $replacements[$safe_state][$token . '[' . $key . ']'] = $replacement;
}

Youssef El Montaser

mikemoretti’s picture

You need to replace:

if(strpos($key, 'field_') !== false) {

with

if(strpos($key, 'field_') !== false && isset($value['und']) {

Thanks!

wferrell343’s picture

Ugh still nothing....

wferrell343’s picture

Ugh still nothing....

micke793’s picture

I have google it, and D7 have another profile module then D6.
And when you create user field in D7 they starts with field_ instead of profile_ :-(

Some one who has a solution to use tokens in D7, I´m working with a webform and wants to get the user adres as a default value from the logged in user, like %profil[field_adres1]

misterbenny’s picture

I have read you can use the Token module to do this, but unfortunately this doesn't work for me. The syntax for the form fields default value should be something like [user:field_adres1].. Let me know if this works for any of you guys..

htnux’s picture

I ran into same problems. It seems no one has found an answer yet, so I went to take a look at the source.
I added few lines to fix the problem. I haven't tested so many times, but it seems working so far.
I added webform.module on line 2854:

              // for custom forms -- added by Y.H. on 2012/02/02 12:13
              if (is_array($value) && empty($value['und'][0]['value']) === false) {
                  $value = $value['und'][0]['value'];
              }

I hope this helps those who faces the same problem.

Ubini’s picture

Thanks you very much, that works for me ! :-)

moonberry’s picture

To get user profile fields in webform token, add this:

file: webform.module
function: There _webform_filter_values()
In line: 2962 ( 7.x-3.x-dev 2012-Apr-16 )
search for:

  // Load profile information if available.
    if ($user->uid) {
      $account = user_load($user->uid);
      $special_tokens['unsafe']['%profile'] = (array) $account;
    }

replace with:

 // Load profile information if available.
    if ($user->uid) {
      $account = user_load($user->uid);
      
      foreach ($account as $k => $v) {      
        if ( is_array($v) && !empty($v['und']['0']['value']) ) {
          $account->$k = $v['und']['0']['value']; 
        }
      }
      
      $special_tokens['unsafe']['%profile'] = (array) $account;
    }

Now You can put any profile fields as default value in forms :)

etiennechataignier’s picture

By doing his, you mess up the $account object, which cannot be used properly by other modules.

Prefer this :

if ($user->uid) {
      $account = user_load($user->uid);
      
      $account_data = array();
      foreach ($account as $k => $v) {      
        if ( is_array($v) && !empty($v[LANGUAGE_NONE]['0']['value']) ) {
          $account_data[$k] = $v[LANGUAGE_NONE]['0']['value']; 
        }
      }
      
      $special_tokens['unsafe']['%profile'] = $account_data;
}
jsimonis’s picture

Made the change to the module. But how do I use the profile fields?

jsimonis’s picture

Ok, I switched to the alpha version that has the token support. Then all I have to do is go to the user settings page where it has the available tokens, copy it, and put it into the default value. Wish the token list showed on the webform, but this isn't too bad.

lupus78’s picture

if you use it like this, it will support multiply default values separated by commas

      // user profile field patch!!!
      $account_data = array();
      foreach ($account as $k => $v) {      
        if ( is_array($v) && !empty($v[LANGUAGE_NONE]['0']['value']) ) {
			$val = array();
			foreach ( $v[LANGUAGE_NONE] as $vv ) $val[] = $vv['value'];
			
			$account_data[$k] = implode(',',$val); 
        }
      }
      
      $special_tokens['unsafe']['%profile'] = $account_data;</code>

the tokens can used like this:
%profile[field_user_last_name] where field_user_last_name is the name of the field

loopy1492’s picture

echatiaig's code works for me. Thanks. Let's hope they add it to the next release.

francois.soulard’s picture

Thanks for sharing this code.
I add a test and a node_load to add entity ref value.
It's awfull but it does the tricks...

if ($user->uid) {
      $account = user_load($user->uid);
      foreach ($account as $k => $v) {
        if ( is_array($v) && !empty($v['und']['0']['value']) ) {
          $account->$k = $v['und']['0']['value'];
        }
        if ( is_array($v) && !empty($v['und']['0']['target_id']) ) {
        	$targetnode = node_load($v['und']['0']['target_id']);
          $account->$k = $targetnode->title;
        }
      }
      $special_tokens['unsafe']['%profile'] = (array) $account;
    }
hawkeye.twolf’s picture

Dave Reid implemented the D7 token system for webform in the 7x-4x branch. Please use the 4x branch of webform for access to proper user tokens. Note that when upgrading from webform 3x to 4x, various database updates are needed, so don't forget to run /update.php.

@see #1001798: Rewrite token replacement system to use D7 tokens

Hawkeye Tenderwolf
a Senior Developer
at Lullabot