Problem/Motivation

Users of User Import may also want to import Profile 2 fields.

On the project page, it says:

Profile 2 fields not yet well tested, this feature is in development.

This is not correct. This feature is not being developed by any of the project's maintainers. While there is a patch provided by users of the module in this issue thread. The principal author of said patch says in comment #22 that:

I'm not using this patch anymore, now I prefer to import only the basic info with user import and then I import the Profile2 part of the profiles with the feeds module. I have to do an extra step, but feeds is much better and reliable.

Based upon that, the patch should not be committed, but alternatives listed in the next section should be recommended on the project page.

Proposed resolution

Those who need do do such imports should use Feeds or this project on GitHub.

Remaining tasks

None

Original report by DarrellDuane

Although I see on the main module page that Profile2 fields are not well tested, I didn't see a formal issue for this task, so I wanted to create a place for us to be able to talk about progress on this issue. Robert, or others, whats the latest?

Comments

scuba_fly’s picture

I don't see any profile2 fields only standard ( email, username, password ) and taxonomy user fields as options available.

My guess is that after you import ( the sample.txt ) you should be able to match those fields to the profile2 fields in "field match" ?

looking in the code I noticed content_profile_get_types() function being called.
I think this was a drupal 6 function and should point to the profile2 version of this function in drupal 7

The drupal 7 version in profile 2 is profile2_get_types()

I see this is in the content_profile.inc file so not sure this is the place to be altert or that a new .inc file should be made?
Any suggestions are welcome of course. I'm willing to contribute to this module but I'm not considering myself a drupal developer expert.

scuba_fly’s picture

I tried adding a profile2.inc in the 'supported' folder

In it I put the following code to start with:

function profile2_user_import_form_field_match() {
    $options                    = array();
    $options['profile2']        = array();
    $field_options              = array();
    $profile2_types = profile2_get_types();
    watchdog('foo','bar');
    debug('6nq3p');
    dsm($profile2_types);
}

But the hook_user_import_form_field_match() is not called...

mrdth’s picture

Flushing caches works to get this new file picked up by the module. I got the profile2 fields listed by using the following in profile2.inc:

function profile2_user_import_form_field_match() {
  $options                    = array();
  $options['profile2']        = array();
  $field_options              = array();
  $supported_field_types = field_user_import_supported_fields();
  $fields_info = field_info_instances('profile2');

  foreach ($fields_info as $bundle => $fields) {
    foreach ($fields as $field_name => $field_data) {
      $field_info = field_info_field($field_name);
      $type       = $field_info['type'];

      if (isset($supported_field_types[$type])) {
        $options['profile2'][$field_name] = $field_data['label'];
      }
      else {
        drupal_set_message(t('Field %field is not supported', array('%field' => $field_data['label'])), 'warning');
      }
    }
  }

  return $options;
}

(Basically combining what you had above with the hook_user_import_form_field_match function from the fields.inc file.)

druipol’s picture

Version: 7.x-1.x-dev » 7.x-2.1
Status: Active » Needs review
StatusFileSize
new1.02 KB

I made an patch from previous comment.

MM10’s picture

Thanks for this... I had some trouble making the Profile2 fields available. I then I realized the patch was supposed to be applied within the support directory... and now it brings up the fields- thanks!

schifazl’s picture

Issue summary: View changes

I don't know if I'm doing something wrong, but it doesn't work for me. When importing I can see the profile2 fields and match them to the CSV file columns, I get no errors, but the profile2 fields aren't filled.

I constantly get this warning, I don't know if it's related to this problem: Warning: Creating default object from empty value in _user_import_settings_save() (line 574 of sites/all/modules/user_import/user_import.module)

armyofda12mnkeys’s picture

Solution #3 brings up the profile2 fields...
But I get the same warning when importing.... and the mapped profile2 fields don't actually get imported and saved to db.
We are using php5.2.17 and haven't gotten a chance to goto 5.3 yet. Do you think thats an issue causing import problems and warning above?

armyofda12mnkeys’s picture

Note: php5.3 makes that warning go away... but still doesn't populate the profile2 fields.
Let me know if any patches/updates I can try if you know what could solve the issue as I can test on my local box anytime.

armyofda12mnkeys’s picture

Comparing now to the other includes, i assume it doesn't work because the profile2.inc file above doesn't include the other hooks that should be in there.
mrdth, do you have the full profile2.inc if you got it working? otherwise I can maybe take a look next weekend at figuring it out.

guntherdevisch’s picture

Hey armyofda12mnkeys,

I also want to import profile2 fields, did you made any progress meanwhile? :)

Thanks,
Gunther

armyofda12mnkeys’s picture

Sorry Gunther, didn't have time to look at it yet and decided to avoid Profile2 for now.

alibama’s picture

likewise - see fields, don't get the data imported - new install, dev modules + patch

ximix’s picture

Alibama,

You have to write code for hook_user_import_after_save

ximix’s picture

I made a patch for complete process.

schifazl’s picture

Status: Needs review » Needs work

It works, great job! But it has two issues:

  1. The path should start from the root of the module, not from the root of the drupal installation
  2. It should be possible to select the target profile, now it imports only in the Main profile
schifazl’s picture

Status: Needs work » Needs review
StatusFileSize
new6.23 KB

OK, I've resolved the two problems found in patch #14, now in the "New import" page, under the Options fieldgroup, you can choose in which Profile type the values will be imported :)

EDIT:
Sorry, I have forgotten that you need to apply also this patch to the Profile2 module: https://www.drupal.org/node/2331435

If you don't want to apply that patch you can simply replace
$profile->defaultLabel();
with
$profile->label;
on the 4th line of the profile2_user_import_form_fieldset function

rooby’s picture

Version: 7.x-2.1 » 7.x-2.x-dev
Category: Task » Feature request

I would really like to see this as major since profile2 has so many users and it so closely relates to the importing of users, but technically feature requests are not major.

rooby’s picture

Status: Needs review » Needs work

A quick code style review (see https://www.drupal.org/coding-standards). I haven't done technical review.

  1. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    + * Implementation of hook_user_import_form_field_match().
    ...
    + * Implementation of hook_user_import_data().                                                                                                                                                                                                                                  ¶
    ...
    + * Implementation of hook_user_import_after_save().                                                                                                                                                                                                                            ¶
    

    These should be "Implements hook_name()." for D7.

  2. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +  $options                    = array();
    +  $options['profile2']        = array();
    +  $field_options              = array();
    +  $supported_field_types = field_user_import_supported_fields();
    +  $fields_info = field_info_instances('profile2');
    

    This is messy. It should either align all equals or not align any of them (my vote is for the latter but it doesn't matter either way).

  3. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +
    ...
    +        if ($module!= 'profile2') return;                                                                                                                                                                                                                                      ¶
    

    We should always use the form:

    <?php
    if ($module != 'profile2') {
      return;
    }
    ?>
  4. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +                                                                                                                                                                                                                                                                               ¶
    +/**                                                                                                                                                                                                                                                                            ¶
    + * Implementation of hook_user_import_data().                                                                                                                                                                                                                                  ¶
    + */                                                                                                                                                                                                                                                                            ¶
    +function profile2_user_import_data($settings, $update_setting, $column_settings, $module, $field_id, $data, $column_id) {                                                                                                                                                      ¶
    +                                                                                                                                                                                                                                                                               ¶
    +        if ($module!= 'profile2') return;                                                                                                                                                                                                                                      ¶
    +        return trim(utf8_encode($data[$column_id]));                                                                                                                                                                                                                           ¶
    +}                                                                                                                                                                                                                                                                              ¶
    +
    +                                                                                                                                                                                                                                                                               ¶
    +/**                                                                                                                                                                                                                                                                            ¶
    + * Implementation of hook_user_import_after_save().                                                                                                                                                                                                                            ¶
    + */                                                                                                                                                                                                                                                                            ¶
    +function profile2_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {                                                                                                                                                      ¶
    

    Lots of trailing whitespace here.

  5. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +function profile2_user_import_data($settings, $update_setting, $column_settings, $module, $field_id, $data, $column_id) {                                                                                                                                                      ¶
    +                                                                                                                                                                                                                                                                               ¶
    +        if ($module!= 'profile2') return;                                                                                                                                                                                                                                      ¶
    

    Unnecessary blank line here and also at the start of most functions in this patch.

  6. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +  foreach($fields['profile2'] as $keyfield => $valuefield){
    

    Missing spaces before ( and after ).

  7. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +  $form = array();
    ...
    +/**
    + * Implementation of hook_user_import_form_fieldset().
    + * Add fieldsets to an import settings form.
    + */
    

    There should be a blank line after the first line short description and any other description.

  8. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +  field_attach_submit('profile2', $profile, $form, $form_state); // attach $profile to profile2 submit
    +  profile2_save($profile); // save profile
    

    Comments should be on the line above the code, not after the code inline. Comments also should be proper sentences, with a capital for the first letter and a full stop at the end.

  9. +++ b/supported/profile2.inc
    @@ -0,0 +1,94 @@
    +  foreach($profiles as $name => $profile){
    

    Missing space before ( and after ).

schifazl’s picture

Thanks! Modified as you suggested and added a mini modification: in profile2_user_import_form_fieldset() the collapsed status is set to false. This is because when you save the import settings, only the profile settings will be saved. IMHO this should be changed and handled by the module, not by supported/user_import.inc

This patch still depends on https://www.drupal.org/node/2331435
Or you can simply replace
$profile->defaultLabel();
with
$profile->label;
on the 4th line of the profile2_user_import_form_fieldset function.
I don't know that to do with this. Wait that the Profile2 guys accept that patch or continue here without the defaultLabel()?

rooby’s picture

Status: Needs work » Needs review
finedesign’s picture

I just tried using the dev version of user import to see if it would import to Profile 2 fields. I don't see them listed after importing a csv file. Is the above approach the only workable solution? I don't know how to run a patch and would appreciate help.

schifazl’s picture

Here you have some info on applying patches.

Anyway I'm not using this patch anymore, now I prefer to import only the basic info with user import and then I import the Profile2 part of the profiles with the feeds module. I have to do an extra step, but feeds is much better and reliable.

Coyote6GraphX’s picture

I know this is old, but for anyone needing to do this still, it doesn't need to be done as a patch. It can be a separate module, that way it doesn't need a patched on each module update.

https://github.com/Coyote6/drupal-7-user-import-profile2

I only briefly tested the code, and it worked for the two fields I was importing. Hope it helps others. Thanks.

gisle’s picture

Issue summary: View changes

Updating issue summary based upon comment #22 and comment #23. Setting statusb to "Fixed". Anyone that disagrees has two weeks to repoen.

gisle’s picture

Issue summary: View changes
Status: Needs review » Fixed

Changing status.

Status: Fixed » Closed (fixed)

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