import_user.module in Import 6
An example use case for the import API written to import users from an existing Drupal installation. Pulls in one profile field.
In lieu of a second data source we will be pulling from an internal test table. A common scenario for imports would involve a second database. Comments below illustrate where you would normally switch datasources.
File
examples/import_user/import_user.moduleView source
<?php
/**
* @file
* An example use case for the import API written to import users from an
* existing Drupal installation. Pulls in one profile field.
*
* In lieu of a second data source we will be pulling from an internal test
* table. A common scenario for imports would involve a second database.
* Comments below illustrate where you would normally switch datasources.
*/
/**
* Implementation of hook_import_stage()
*
*/
function import_user_import_stage() {
// The import_user_staged variable is set to false during the installation of
// this module.
if (variable_get('import_user_staged', FALSE) == FALSE) {
// select all of the old uids we will be importing. If you were importing
// from an external datasource you would use db_set_active() to switch
// before running the select statement.
$sql = "SELECT id from {import_example_user}";
$result = db_query($sql);
while ($user = db_fetch_object($result)) {
// create an array containing identifier and type both of which are used in
// this modules process hook. The type is an arbitrary identifer the
// developer can use to easily identify which type of data is referenced.
// The type chosen by the developer will get passed to this modules
// implementation of hook_import_process.
$attr = array();
$attr['impid'] = $user->id;
$attr['type'] = 'user';
// pass the array to the import_stage method
import_stage($attr);
}
// set this variable to true to eliminate restaging the data
variable_set('import_user_staged', TRUE);
}
}
/**
* Implementation hook_import_process()
*/
function import_user_import_process($data) {
switch ($data->type) {
case 'user':
$new_user = _import_user_map_data($data);
$obj = new stdClass();
$account = user_save($obj, $new_user);
if ($account === FALSE) {
import_fail($data, 'User Import failed' . print_r($new_user, TRUE));
}
else {
import_pass($data, 'User Import passed' . print_r($account, TRUE));
}
break;
}
}
/**
* Helper function to map the old data to a Drupal user object;
*/
function _import_user_map_data($data) {
// First we need to select the old record we are dealing with. Normally this
// would be done by using db_set_active() to switch datasources.
$sql_old_user = "SELECT id, email, name, random_username, random_password from {import_example_user} WHERE id = %d";
$old_user = db_fetch_object(db_query($sql_old_user, $data->impid));
$account = array();
$account['status'] = 1;
$account['roles'] = array(
DRUPAL_AUTHENTICATED_RID,
);
$account['name'] = $old_user->random_username;
$account['mail'] = $old_user->email;
$account['pass'] = $old_user->random_password;
$account['profile_name'] = $old_user->name;
$account['profile_original_id'] = $old_user->id;
return $account;
}
Functions
Name | Description |
---|---|
import_user_import_process | Implementation hook_import_process() |
import_user_import_stage | Implementation of hook_import_stage() |
_import_user_map_data | Helper function to map the old data to a Drupal user object; |