Does the custom field example in the module's main page still holds for 7.0?

I tried it but it didnt't work.

Thank you for the great module.

CommentFileSizeAuthor
#8 uif_custom.zip4.25 KBbensnyder

Comments

dwightaspinwall’s picture

Category: bug » support

Hi - the example on the project page is for drupal 6, using the optional Profile module which is part of core. The Profile module has been deprecated in drupal 7, since you can add fields to users. Or optionally, you could look at Profile2 module, which is the Drupal 7 successor to Profile - http://drupal.org/project/profile2

Regardless of which approach you take the idea should be the same. You're going to use hook_uif_pre_create() to assign data into the field. The trick is to find out the name of the variable in the user object. And for that I suggest the Devel module, which puts a tab on user/ which shows a dump of the user object. So, just add the fields, populate a user with some data via the UI, then use user//devel to find out the variable naming convention. Then use that convention in your hook.

alarfaj’s picture

Thank you,

I used something like this and it worked.

$lang = LANGUAGE_NONE;
$profile['field_fname'][$lang][0]['value'] = $user_data['fname'];

dwightaspinwall’s picture

Status: Active » Closed (works as designed)

Cool. Glad you got it working.

dan3h’s picture

Status: Closed (works as designed) » Needs review

The solution above didn't work for me -- I think it is for drupal 6, or for the old (deprecated) profiles module which it is still possible to use in drupal 7. I am using the Profile2 module, and it has a different API.

Here is what worked for me with Profile2. I'm leaving out the code which imports the normal user fields (username, password, etc... anything that resides in the 'users' table) -- that part works already, as per the code example that comes with the uif_example.module. My code here is the part that deals with profile2 fields. It seems that first, you have to create the user objects, and then you can add the profile2 field values. That's why I'm using the hook_uif_post_* functions:

/**
 * Implementation of hook_uif_post_create().
 */
function mymodule_uif_post_create($account, $user_data) {
  $profile = profile_create(array('user' => $account, 'type' => 'my_profile_type'));
  _mymodule_assign_profile2_fields($profile, $user_data);
}

/**
 * Implementation of hook_uif_post_update().
 */
function mymodule_uif_post_update($account, $user_data) {
  $profile = profile2_load_by_user($account, 'my_profile_type');
  _mymodule_assign_profile2_fields($profile, $user_data);
}

// Code shared by the ..._post_create and ..._post_update functions.
function _mymodule_assign_profile2_fields($profile, $user_data) {
  $profile->field_firstname[LANGUAGE_NONE][0]['value'] = $user_data['first_name'];
  $profile->field_lastname[LANGUAGE_NONE][0]['value'] = $user_data['last_name'];
  profile2_save($profile);
}
bensnyder’s picture

Version: 7.x-1.0-beta1 » 7.x-1.0-beta5
Status: Closed (works as designed) » Needs review

thanks @dan3h your code helped me do a successful mapping/import

dwightaspinwall’s picture

Status: Needs review » Closed (works as designed)

Glad we got resolution on this. Added note to project page doc.

shadowdknight’s picture

Hi
Can anyone provide with full piece of code please,
Im confuse , where should I add the working piece.

For example my profile 2 machine name is : main
additional field is: field_phone

Thanks!

bensnyder’s picture

StatusFileSize
new4.25 KB

@shadowhitman #7

Here's the full code that worked for me; it's a custom module. It's still got the field names I used- so you need to change it to fit your needs.

bensnyder’s picture

OH! Take note!

I added
realname_update($account);

Which requires the realname module. You need to either remove that code (I think I added it twice in the .module) or have realname.module set up and ready.

Good luck

shadowdknight’s picture

It works!!!
Thanks you so much!

wooody’s picture

Thanks , works with me

funkeyrandy’s picture

Status: Needs review » Closed (works as designed)

very cool. How about assigning multiple roles from the csv import? mine are "|" separated: admin|subscriber| etc

dwightaspinwall’s picture

Easily done in a hook_uif_post_create and/or hook_uif_post_update funkeyrandy. Just update the user and call user_save(). Hope that helps.

Next time, please open a new issue, since your comment was unrelated. Thanks.

funkeyrandy’s picture

in drupal 7, what would be the best way to get regular profile fields?
is the standard profile saved along with user_save ? it seems it must be different than:
$profile->field_profile_fname[LANGUAGE_NONE][0]['value'] = $user_data['fname'];

i thought this would do it:

 function uif_example_assign_fields($user_data) {
  

  $fields = array();
  $fields['field_phone'][LANGUAGE_NONE][0]['value'] = $user_data['phone'];


  return $fields;
}

thanks!

funkeyrandy’s picture

nevermind...figured it out with:
$account->field_phone[LANGUAGE_NONE][0]['value'] = $phone;

dwightaspinwall’s picture

Ben,
Thanks for the module in http://drupal.org/node/1047266#comment-5308174 Inspired me to add a new example module to the project to show profile 2 support. Going into dev today, and will be in 7.x-1.1 and later.

bensnyder’s picture

@dwightaspinwall - Glad to be of service and thank YOU for all the work YOU'VE done.