Migrating content profiles to Profile2

Tue, 08/14/2012 - 09:15 -- John Locke

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:

1. 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.
2. Check the boxes for all the items to migrate.
3. Select "Execute Arbitrary PHP Script"
4. Paste in the above code, adjusting it for the fields you have on your content profile and profile2.
5. Execute, and test!

Hope that helps!

Story Type: 

Comments

Submitted by fugazi (not verified) on

I've been searching and experimenting. Their solution was the success. Thank you very much

Submitted by Peter (not verified) on

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.

Submitted by Vinoth (not verified) on

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

  1. I recommend you use Linux for your server(s). Mine are so reliable, it shocked me that after years of Microsoft-based expectations, I have no complaints now after many many years experience with Linux servers supporting a mixed Win2K and Apple OSX workstation network. Freelock has really opened my eyes to what I should be expecting from enterprise software. Linux is simply much better than anything Microsoft has done, and even on Microsoft's best day, Microsoft is too expensive, too proprietary and too unreliable. There is just no reason to keep putting ourself through that grief, constant change, and endless high cost.

    George Roberston
    George Roberston & Associates

Need More Freelock

       

About Freelock

We are located in Pioneer Square, in downtown Seattle. 83 Columbia Street #401 Seattle, WA 98104  USA [P] 206.577.0540 Contact Us/Directions | Site Map Get Updates ©1995-2014 Freelock Computing