public function MigrateDestinationUser::import in Migrate 6.2
Same name and namespace in other branches
- 7.2 plugins/destinations/user.inc \MigrateDestinationUser::import()
Import a single user.
Parameters
$account: Account object to build. Prefilled with any fields mapped in the Migration.
$row: Raw source data object - passed through to prepare/complete handlers.
Return value
array Array of key fields (uid only in this case) of the user that was saved if successful. FALSE on failure.
Overrides MigrateDestination::import
File
- plugins/
destinations/ user.inc, line 124 - Support for user destinations.
Class
- MigrateDestinationUser
- Destination class implementing migration into users.
Code
public function import(stdClass $account, stdClass $row) {
$migration = Migration::currentMigration();
// Updating previously-migrated content?
if (isset($row->migrate_map_destid1)) {
if (isset($account->uid)) {
if ($account->uid != $row->migrate_map_destid1) {
throw new MigrateException(t("Incoming uid !uid and map destination uid !destid1 don't match", array(
'!uid' => $account->uid,
'!destid1' => $row->migrate_map_destid1,
)));
}
}
else {
$account->uid = $row->migrate_map_destid1;
}
}
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
if (!isset($account->uid)) {
throw new MigrateException(t('System-of-record is DESTINATION, but no destination uid provided'));
}
$old_account = user_load($account->uid, TRUE);
if (empty($old_account)) {
throw new MigrateException(t('System-of-record is DESTINATION, but user !uid does not exist', array(
'!uid' => $account->uid,
)));
}
}
else {
if (!isset($account->status)) {
$account->status = 1;
}
$old_account = $account;
}
// Convert core date columns to unix timestamp as needed.
foreach (array(
'created',
'access',
'login',
) as $property) {
if (isset($account->{$property})) {
$account->{$property} = MigrationBase::timestamp($account->{$property});
}
else {
// Don't touch in the DESTINATION case, preserve the original value
if ($migration
->getSystemOfRecord() != Migration::DESTINATION) {
$account->{$property} = $_SERVER['REQUEST_TIME'];
}
}
}
// Roles must be arrays keyed by the role id, which isn't how the data
// naturally comes in. Fix them up.
// First, if names instead of IDs are presented, translate them
if (!empty($account->role_names)) {
$role_names = is_array($account->role_names) ? $account->role_names : array(
$account->role_names,
);
foreach ($role_names as $role_name) {
$rid = db_select('role', 'r')
->fields('r', array(
'rid',
))
->condition('name', $role_name)
->execute()
->fetchField();
if ($rid) {
$account->roles[] = $rid;
}
}
}
if (!empty($account->roles)) {
if (!is_array($account->roles)) {
$account->roles = array(
$account->roles,
);
}
$account->roles = drupal_map_assoc($account->roles);
}
if (empty($account->roles) && empty($old_account->roles)) {
$account->roles = array();
}
$this
->prepare($account, $row);
if (isset($account->uid) && !(isset($account->is_new) && $account->is_new)) {
$updating = TRUE;
}
else {
$updating = FALSE;
// Prepare object for a new user to be passed in user_save.
$old_account->uid = NULL;
}
// If any datetime values were included, ensure that they're in timestamp format.
if (isset($account->created)) {
$account->created = MigrationBase::timestamp($account->created);
}
if (isset($account->access)) {
$account->access = MigrationBase::timestamp($account->access);
}
if (isset($account->login)) {
$account->login = MigrationBase::timestamp($account->login);
}
migrate_instrument_start('user_save');
$newaccount = user_save($old_account, (array) $account);
migrate_instrument_stop('user_save');
if ($newaccount) {
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
$this
->complete($newaccount, $row);
$return = array(
$newaccount->uid,
);
}
else {
$return = FALSE;
}
return $return;
}