Skip to content

Instantly share code, notes, and snippets.

@fmitchell
Last active November 14, 2016 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fmitchell/6079933 to your computer and use it in GitHub Desktop.
Save fmitchell/6079933 to your computer and use it in GitHub Desktop.
Migrate example using migrate, migrate_d2d, migrate_extras
<?php
/**
* This is example code for a Drupal 6 to Drupal 7 migration. This won't actually
* work without defining the referenced vocabularies, content types, and fields
* on each side.
*/
/**
* Implements hook_flush_caches().
*/
function whg_migrate_flush_caches() {
whg_migrate_register_migrations();
}
/**
* Register all D6->D7 migrations.
*/
function whg_migrate_register_migrations() {
/**
* Each migration being registered takes an array of arguments, some required
* and some optional. Start with the common arguments required by all - the
* source_connection (connection key, set up in settings.php, pointing to
* the Drupal 6 database) and source_version (major version of Drupal).
*/
$common_arguments = array(
'source_connection' => 'legacy',
'source_version' => 6,
);
// Migrate roles
$role_arguments = $common_arguments + array(
'machine_name' => 'WHGRole',
'description' => t('Import Drupal 6 roles'),
'role_mappings' => array(
'administrator' => 'administrator',
'super administrator' => 'administrator',
'coach' => 'Coach',
'case manager' => 'Case Manager',
'client' => 'Client',
'reviewer' => 'Reviewer',
),
);
Migration::registerMigration('DrupalRole6Migration',
$role_arguments['machine_name'], $role_arguments);
$arguments = $common_arguments + array(
'description' => t('Migration of users from Drupal 6'),
'machine_name' => 'WHGUser',
);
Migration::registerMigration('WHGUserActualMigration',
$arguments['machine_name'], $arguments);
$record_arguments = $common_arguments + array(
'description' => t('Migration of record to user data'),
'machine_name' => 'WHGRecord',
);
Migration::registerMigration('WHGRecordMigration',
$record_arguments['machine_name'], $record_arguments);
$name_arguments = $common_arguments + array(
'description' => t('Migration of name, legacy record to user data'),
'machine_name' => 'WHGName',
);
Migration::registerMigration('WHGNameMigration',
$name_arguments['machine_name'], $name_arguments);
$node_arguments = $common_arguments + array(
'class_name' => 'WHGCMRMigration',
'description' => t('Migration of CMR nodes from Drupal 6'),
'machine_name' => 'WHGCMR',
'source_type' => 'case_management_report',
'destination_type' => 'cmr',
'user_migration' => 'WHGUser'
);
Migration::registerMigration($node_arguments['class_name'],
$node_arguments['machine_name'], $node_arguments);
$fee_arguments = $common_arguments + array(
'class_name' => 'WHGFeesMigration',
'description' => t('Migration of Non-Coaching Fee nodes from Drupal 6'),
'machine_name' => 'WHGFees',
'source_type' => 'non_coaching_fees',
'destination_type' => 'non_coaching_fees',
'user_migration' => 'WHGUser'
);
Migration::registerMigration($fee_arguments['class_name'],
$fee_arguments['machine_name'], $fee_arguments);
$profile_arguments = $common_arguments + array(
'description' => t('Migration of member profile to user data'),
'machine_name' => 'WHGProfile',
);
Migration::registerMigration('WHGProfileMigration',
$profile_arguments['machine_name'], $profile_arguments);
}
/**
* Implements hook_migrate_api().
*/
function whg_migrate_migrate_api() {
$api = array(
'api' => 2,
);
return $api;
}
/**
* Retrieve the new id from the old id
*/
function whg_migrate_get_new_id($class, $id) {
$query = db_select('migrate_map_' . $class, 'm')
->fields('m', array('destid1'))
->condition('m.sourceid1', $id, '=');
$result = $query->execute();
$output = '';
foreach ($result as $row) {
$output = $row->destid1;
}
return $output;
}
<?php
class WHGRecordMigration extends Migration {
public function __construct() {
parent::__construct();
$this->description = t('Migrate record nodes to user data.');
$this->dependencies = array('WHGUser');
$this->systemOfRecord = Migration::DESTINATION;
$this->map = new MigrateSQLMap($this->machineName,
array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'D6 Unique Node ID',
'alias' => 'n',
)
),
DrupalDestinationUser::getKeySchema()
);
$query = Database::getConnection('default', 'legacy')
->select('node', 'n')
->fields('n', array('nid'))
->condition('n.type', 'record', '=')
->condition('n.status', 1, '=');
$query->join('content_type_record', 'ctr', 'n.nid = ctr.nid');
$query->addField('ctr', 'field_client_level_value');
$query->addField('ctr', 'field_user_account_uid');
$query->addField('ctr', 'field_gender_value');
$query->addField('ctr', 'field_language_preference_value');
$query->join('content_field_follow_up', 'cffu', 'n.nid = cffu.nid');
$query->addField('cffu', 'field_follow_up_value');
$query->addField('ctr', 'field_follow_up_notes_value');
$query->addField('ctr', 'field_company_value');
$query->addField('ctr', 'field_worksheet_date_value');
$query->addField('ctr', 'field_waiver_date_value');
$query->addField('ctr', 'field_letter_presented_value');
$query->addField('ctr', 'field_sep_date_value');
$query->addField('ctr', 'field_assignment_date_value');
$query->addField('ctr', 'field_eid_value');
$query->addField('ctr', 'field_region_value');
$query->addField('ctr', 'field_department_value');
$query->addField('ctr', 'field_supervisor_name_value');
$query->addField('ctr', 'field_supervisor_title_value');
$query->addField('ctr', 'field_supervisor_email_email');
$query->addField('ctr', 'field_supervisor_phone_value');
$query->addField('ctr', 'field_client_id_legacy_value');
$query->addField('ctr', 'field_coach_profile_nid');
// Add source fields which not queried in $query, will be populated in prepareRow()
$source_fields = array(
'ref_account_uid' => t('user reference on record'),
'ref_coach_uid' => t('coach user reference on record'),
);
// Create a MigrateSource object, which manages retrieving the input data.
$this->source = new MigrateSourceSQL($query, $source_fields);
$this->destination = new DrupalDestinationUser();
$this->addFieldMapping('uid', 'ref_account_uid');
$this->addFieldMapping('field_user_coach', 'ref_coach_uid');
$this->addFieldMapping('field_user_client_level', 'field_client_level_value');
$this->addFieldMapping('field_user_client_id', 'nid');
$this->addFieldMapping('field_user_gender', 'field_gender_value');
$this->addFieldMapping('field_user_language', 'field_language_preference_value');
$this->addFieldMapping('field_user_follow_up', 'field_follow_up_value');
$this->addFieldMapping('field_user_follow_up_notes', 'field_follow_up_notes_value');
$this->addFieldMapping('field_user_company', 'field_company_value');
$this->addFieldMapping('field_user_worksheet_received', 'field_worksheet_date_value');
$this->addFieldMapping('field_user_waiver_auth_received', 'field_waiver_date_value');
$this->addFieldMapping('field_user_letter_presented', 'field_letter_presented_value');
$this->addFieldMapping('field_user_separation_date', 'field_sep_date_value');
$this->addFieldMapping('field_user_assignment_date', 'field_assignment_date_value');
$this->addFieldMapping('field_user_client_eid', 'field_eid_value');
$this->addFieldMapping('field_user_region', 'field_region_value');
$this->addFieldMapping('field_user_department', 'field_department_value');
$this->addFieldMapping('field_user_supervisor_name', 'field_supervisor_name_value');
$this->addFieldMapping('field_user_supervisor_title', 'field_supervisor_title_value');
$this->addFieldMapping('field_user_supervisor_email', 'field_supervisor_email_email');
$this->addFieldMapping('field_user_supervisor_phone', 'field_supervisor_phone_value');
$this->addFieldMapping('field_user_client_id_legacy', 'field_client_id_legacy_value');
$this->addUnmigratedDestinations(array(
'is_new',
'mail',
'name',
'pass',
'status',
'created',
'access',
'login',
'roles',
'picture',
'role_names',
'signature',
'signature_format',
'timezone',
'language',
'theme',
'init',
'data',
'path',
'pathauto',
'field_user_language:language',
'field_user_follow_up_notes:language',
'field_user_client_eid:language',
'field_user_region:language',
'field_user_department:language',
'field_user_supervisor_name:language',
'field_user_supervisor_title:language',
'field_user_supervisor_phone:language',
'field_user_firstname',
'field_user_firstname:language',
'field_user_lastname',
'field_user_lastname:language',
'field_user_address',
'field_user_phone',
'field_user_phone:language',
'field_user_coach_designation',
'field_user_coach_designation:language',
'field_user_coach_biography',
'field_user_coach_biography:format',
'field_user_coach_biography:language',
'field_user_coach_experience',
'field_user_coach_experience:format',
'field_user_coach_experience:language',
'field_user_coach_expertise',
'field_user_coach_expertise:format',
'field_user_coach_expertise:language',
'field_user_coach_education',
'field_user_coach_education:language',
'field_user_coach_education:format',
'field_user_coach_publication',
'field_user_coach_publication:format',
'field_user_coach_publication:language',
'field_user_coach_membership',
'field_user_coach_membership:language',
'field_user_coach_membership:format',
), t('Do Not Migrate'));
}
public function prepareRow($current_row) {
$current_row->ref_account_uid = whg_migrate_get_new_id('whguser', $current_row->field_user_account_uid);
$ctr = Database::getConnection('default', 'legacy')
->select('content_type_record', 'ctr')
->fields('ctr', array('field_coach_profile_nid'))
->condition('ctr.nid', $current_row->nid)
->execute()->fetchObject();
if ($ctr) {
$node = Database::getConnection('default', 'legacy')
->select('node', 'n')
->fields('n', array('uid'))
->condition('n.nid', $ctr->field_coach_profile_nid)
->execute()->fetchObject();
if ($node && !empty($node->uid)) {
$current_row->ref_coach_uid = whg_migrate_get_new_id('whguser', $node->uid);
}
else {
$this->saveMessage(t('No user account in record data for cmr node with nid !nid',
array('!nid' => $ctr->field_coach_profile_nid)));
}
}
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment