Skip to main content

Migrating Profile2 and User Data

Back-end Development
Drupal

Migrating a Drupal site can be quite complex.

Fortunately, the migrate module simplifies the process, especially when combined with templates from migrate_extras and migrate_d2d.  I needed a way to migrate user and profile2 data to a new site and found these resources to be very helpful.

Migrate users and files with migrate_d2d

Migrating users from one drupal site to another is simple when using the migrate_d2d module.  Using the example included in the module as a template, just update source_connection and source_version to values specific to your migration in the migrate_d2d_register_migrations function:

/**
 * Implements hook_flush_caches().
 */
function migrate_d2d_flush_caches() {
   migrate_d2d_register_migrations();
}

/**
 * Register all D7->D7 migrations.
 */
function migrate_d2d_register_migrations() {
   $common_arguments = array(
      'source_connection' => 'old_site_database',
      'source_version' => 7,
   );

   $arguments = $common_arguments + array(
      'description' => t('Migration of users from old site to new site'),
      'machine_name' => 'User',
   );
   
   Migration::registerMigration('DrupalUser7Migration', $arguments['machine_name'], $arguments);
}

If you have photo fields associated with your profiles, migrate_d2d helps with the file migration as well.  Add this to your migrate_d2d_register_migrations function:

   // File migration 
   $file_arguments = $common_arguments + array(
      'description' => t('Migrate all files'),
      'machine_name' => 'Files',
      'source_dir' => '/path/to/files',
      'destination_dir' => '',
   );

   Migration::registerMigration('DrupalFile7Migration', $file_arguments['machine_name'], $file_arguments);

NOTE: Make sure the '/path/to/files' directory permissions are set up to allow read/write actions

Migrate profile2 fields with migrate_extras

Now that there are migrations for users and files, we can migrate the rest of the profile2 fields.  The easiest way to do this is create a migration based off of the migrate_examples profile2 migration template included in the module.

class NameOfYourProfile2Migration extends Migration {
   public function __construct() {
      parent::__construct();
      $this->description = t('Migrate profile2 entities');
      $this->dependencies = array('User', 'Files');

      $this->map = new MigrateSQLMap($this->machineName,
         array(
            'pid' => array(
               'type' => 'int',
               'unsigned' => TRUE,
               'not null' => TRUE,
            )
         ),
         MigrateDestinationProfile2::getKeySchema()
      );

      $query = Database::getConnection('default', 'name_of_old_database')
         ->select('profile', 'p')
         ->fields('p', array('pid','uid'))
         ->condition('p.type', 'type_of_profile_you_are_migrating');

      // Add tables and fields to your query to include all of your profile2 fields
      $query->leftjoin('field_data_field_name_of_your_table', 'fdfnoyt', 'fdfnoyt.entity_id = p.pid');
      $query->addField('fdfnoyt', 'field_name_of_your_source_field');
      $query->addField('fdfnoyt', 'field_name_of_your_other_source_field');
      $query->leftjoin('field_data_field_name_of_your_photo_field_table', 'fdfnoypft', 'fdfnoypft.entity_id = p.pid');
      $query->addField('fdfnoypft', 'field_name_of_your_source_photo_field_fid');
      $query->addField('fdfnoypft', 'field_name_of_your_source_photo_field_alt');
      $query->addField('fdfnoypft', 'field_name_of_your_source_photo_field_title');
      
      $this->source = new MigrateSourceSQL($query);
      $this->destination = new MigrateDestinationProfile2('type_of_profile_you_are_migrating');
      
      // Add user mapping
      $this->addFieldMapping('uid', 'uid')->sourceMigration('User');
      $this->addFieldMapping('revision_uid', 'uid')->sourceMigration('User');

      // Add field mappings
      $this->addFieldMapping('field_name_of_your_destination_field','field_name_of_your_source_field');
      $this->addFieldMapping('field_name_of_your_other_destination_field','field_name_of_your_other_source_field');
      
      // Add photo field mappings
      $this->addFieldMapping('field_name_of_your_destination_photo_field', 'field_name_of_your_source_photo_field_fid')
         ->sourceMigration('Files');
      $this->addFieldMapping('field_name_of_your_destination_photo_field:file_class')
         ->defaultValue('MigrateFileFid');
      $this->addFieldMapping('field_name_of_your_destination_photo_field:alt', 'field_name_of_your_source_photo_field_alt');
      $this->addFieldMapping('field_name_of_your_destination_photo_field:title',
         'field_name_of_your_source_photo_field_title');
   }
}

Further reading

Here are some resources that helped me through the migration process: