As part of our recent site upgrade from Drupal 6 to Drupal 7, we had a bunch of content profiles to clean up. Content Profile is a Drupal 6 module that allows you to create "Profile" content types, and we've used that on a bunch of sites for things like directory information, staff biographies, board biographies, etc. And in Drupal 7, it's replaced by Profile2. Profile2 stores profile data in its own entities, not in nodes. And there's no direct path for getting your data from a content profile to a profile2. VBO to the rescue! Views Bulk Operations (VBO) with a little PHP snippet is perfect for this task. The challenge is figuring out the PHP you need to use.
Set up Profile2
First thing, get your new profile2 profiles set up. Profile2 does not let you add the old body fields directly, and does not have a title field. So if you store the user's name in the title and biography in the body, you will need to create and add these fields to your profile2 type. Then add all of the rest of your existing fields to the type.
Migrate the data using "Execute Arbitrary PHP script"
The following snippet migrates data from an "employee" content profile to an "employee" profile2, and it first loads an existing profile2 before updating values -- so you can experiment with values first. To develop this snippet, I created a profile2 for one user with dummy values, and then used "dpm()" (from the devel module) to see its structure and figure out exactly how to make this work. So this snippet replaces the dummy data on existing profiles with the data from the original content_profile:
$newprofile = profile2_load_by_user($entity->uid, 'employee');
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle','employee')
->propertyCondition('uid',$entity->uid);
$result = $query->execute();
if ($result && $result['node']) {
$item = array_pop($result['node']);
$node = node_load($item->nid);
if (!$newprofile) {
$newprofile = profile2_create(array('type'=>'employee','uid'=>$entity->uid));
}
$newprofile->field_name['und'][0]['value'] = $node->title;
$newprofile->field_bio = $node->body;
$newprofile->field_petprojects = $node->field_petprojects;
$newprofile->field_profileimage = $node->field_profileimage;
$newprofile->field_weight = $node->field_weight;
profile2_save($newprofile);
}
To use this snippet:
- In a VBO view of content, select all of the content profiles to migrate. Be sure your view is set up to use batch operations if you have a lot of data.
- Check the boxes for all the items to migrate.
- Select "Execute Arbitrary PHP Script"
- Paste in the above code, adjusting it for the fields you have on your content profile and profile2.
- Execute, and test! Hope that helps!
Thank you very much
I've been searching and experimenting. Their solution was the success. Thank you very much
Drupal 2 Drupal migration module
Or check out the Drupal 2 Drupal migration module to migrate old content profile fields to native Drupal 7 user fields.
http://drupal.org/project/migrate_d2d
A suggested improvement?
It would make the code easier to follow if the old profile and the new profile had different names (eg "employee" and "employee2") so it would be clearer where the values should be edited to match our set-up. And edit the instructions to match of course. You may be surprised how difficult it is for some of us to decipher code and every little hint will help!
Thanks for this code - it has worked once for me but I now need to import into a profile2 with a slightly different name from the original profile and that's not working yet.
Drush commands
FYI I'm working on a sandbox to do the migration, any input would be appreciated: http://drupal.org/sandbox/damienmckenna/1858528
my Code
uid, 'profile');
if (!$profile) {
$profile = profile2_create(array('type'=>'profile','uid'=>$entity->uid));
}
foreach($entity as $f => $fdata){
$profile->$f = $fdata;
}
$profile->field_name['und'][0]['value'] = $entity->title;
$profile->field_profile_body = $entity->body;
profile2_save($profile);
$entity->status = 0;
node_save($entity);
?>
Add new comment