Migrating from Drupal 6 Content Profile to Drupal 7 Account Settings

06 Jun 2013

Posted by Darrell
Darrell's picture

While I've seen a lot of talk about going from Drupal 6 Content Profile to Drupal 7 Profile 2, I haven't seen much in the way of how to go directly to Drupal 7's User Account Settings.

Here I describe and offer a code snippet for doing just this:
Step 1 involves going to http://yoursite.com/admin/config/people/accounts/fields and adding the existing fields from the content profile data types that you want to migrate to your D7 Account Settings.
Simply select each field from the various content profile content types that you want as existing fields for the D7 Account settings, and then order them as you want them to appear on the page.

Step 2 is running the version of the script below to convert the existing data that is of entity_type 'node' to be of entity_type and bundle type 'user', and also updating the entity_id and revision_id to be equal to the uid
of the user that own them.

Here is a sanitized version of the function that I executed to perform this task

////////////////////////////////////////////////////////////////////////////////
// BACK UP YOUR DATA BEFORE RUNNING THIS FUNCTION
// This function takes the listed field names in the array at the top and goes one by one
////////////////////////////////////////////////////////////////////////////////
function mymodule_update_users(){

$fields=array(,
'first_name',
'last_name',
'mobile_phone',
'another_phone',
'contact_notes',
);

// SELECT uid, nid FROM node WHERE type = 'content_profile'
$query = db_select('node', 'n')
->fields('n', array('uid','nid'));
$result = $query
->condition('type', 'content_profile') // this could be done for any content type that you wanted to import in
->execute()->fetchAll();

$i=0;
foreach ($result as $row) {
$i++;

foreach($fields AS $field_suffix){
$table_name = 'field_data_field_' . $field_suffix;

$query3 = db_select($table_name, 'tn')
->fields('tn', array('entity_type'));
$result3 = $query3->condition('entity_type', 'user')
->condition('entity_id', $row->uid)
->execute()->fetchAll();
// ds($result3);

if($result3 == array() ){

// example of UPDATE . $table_name . " SET entity_type = 'user', bundle = 'user', entity_id=1, revision_id = 1 WHERE entity_id = 321
$query2 = db_update($table_name)
->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'entity_id' => $row->uid,
'revision_id' => $row->uid,
));
$result2 = $query2
->condition('entity_id', $row->nid)
->execute();
} // done if this entity hasn't already been migrated
} // done iterating through each table
} // done iterating through each user

} // done moving the content profile data to user account data

Note that this will remove the data from the content_profile data type.