Migrate Classes: Content Profile to Profile2

This installment of the Migrate Classes series will give you a bit of sample code for migrating Content Profile nodes (D6) to Profile2 entities (D7).

Please note that this is not a tutorial for the Migrate module. If you'd like a detailed explanation of the Migrate API, please check either the examples in the Migrate module, or read this excellent blog post on the migrate module.

Here's a bare bones migration class that will query a base node table, join the necessary CCK field table, and migrate them into a Profile2 entity. Please note that you will require the Migrate Extras module to perform this migration.

You'll also notice that this migration depends on another migration, grasmashUser, which will first populate the {users} table. Please see BTMash's example module to see an example of a user migration class.

<?php
/**
* @file
* Examples and test fodder for migration into profile2 entities.
*/

class grasmashProfile2Migration extends Migration {
  public function
__construct() {
   
parent::__construct();
   
$this->description = t('Migration of customer profiles into profile2 entities');
   
$this->dependencies = array('grasmashUser');

    $this->map = new MigrateSQLMap($this->machineName,
      array(
       
'nid' => array(
         
'type' => 'int',
         
'unsigned' => TRUE,
         
'not null' => TRUE,
         
'description' => 'D6 Unique Node ID',
         
'alias' => 'n',
        )
      ),
     
MigrateDestinationProfile2::getKeySchema()
    );

    $query = db_select(grasmash_MIGRATION_DATABASE_NAME . '.node', 'n')
      ->
fields('n', array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'moderate', 'sticky', 'tnid', 'translate'))
      ->
condition('n.type', 'profile', '=');
   
$query->join(grasmash_MIGRATION_DATABASE_NAME . '.node_revisions', 'nr', 'n.vid = nr.vid');
   
$query->addField('nr', 'body');
   
$query->addField('nr', 'teaser');
   
$query->addField('nr', 'format');
   
$query->join(grasmash_MIGRATION_DATABASE_NAME . '.users', 'u', 'n.uid = u.uid');
   
$query->addField('u', 'name');
   
$query->leftJoin(grasmash_MIGRATION_DATABASE_NAME . '.content_type_profile', 'ctp', 'n.nid = ctp.nid AND n.vid = ctp.vid');
   
$query->addField('ctp','field_name_last');
   
$query->addField('ctp','field_name_first');
   
$query->addField('ctp','field_name_middle');
   
$query->addField('ctp','field_name_prefix');
   
$query->addField('ctp','field_name_suffix');
   
$query->orderBy('n.nid', 'ASC');

    $source_fields = array(
     
'nid' => t('The node ID of the page'),
     
'uid' => t('The user ID of a user'),
     
'lid' => t('The location ID of a location instance'),
    );

    // Create a MigrateSource object, which manages retrieving the input data.
   
$this->source = new MigrateSourceSQL($query, $source_fields);
   
$this->destination = new MigrateDestinationProfile2('customer');

    // Add a few field mappings.
   
$this->addFieldMapping('field_name_first', 'field_name_first');
   
$this->addFieldMapping('field_name_last', 'field_name_last');
      
   
// Unmapped destination fields
   
$this->addUnmigratedDestinations(array('id'));
   
$this->addUnmigratedSources(array('vid', 'type', 'language', 'moderate', 'tnid', 'translate', 'teaser', 'format', 'name'));
  }
}
?>

Drupal Version Compatibility: 

Comments

It wasn't clear to me that after installing this that I actually had to use the Migration Module to register this class in its system and then initiate it from the Migration control panel. I hope this note will clarify that for someone.

Hey,

I have migrated my existing site from D6 to D7 and want to migrate the "content profile" content type to profile2. This blog really seems helpful, but i ain't sure how and where to place this piece of code.

@ Darell,

how do you "register this class in its system and then initiate it from the Migration control panel".

help appreciated.

Thanks,
Shabbir

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.