You are here

import_role.module in Import 6

An example use case for the import API written to import users from an existing Drupal installation.

In order for this to work you will likely need to setup a second datasource in settings.php to perform the selects from.

File

examples/import_role/import_role.module
View source
<?php

/**
 * @file
 * An example use case for the import API written to import users from an
 * existing Drupal installation.
 *
 * In order for this to work you will likely need to setup a second datasource
 * in settings.php to perform the selects from.
 */

/**
 * hook_import_stage()
 *
 */
function import_role_import_stage() {
  if (variable_get('import_role_staged', FALSE) == FALSE) {

    // select all of the old uids we will be importing.  Use the datasource
    // we are importing from
    db_set_active('old');
    $sql = "SELECT rid, name from role";
    $result = db_query($sql);
    db_set_active('default');
    while ($role = db_fetch_object($result)) {

      // create an array containing identifier and type both of which are used in
      // this modules process hook
      $attr = array();
      $attr['impid'] = $role->rid;
      $attr['type'] = 'role';
      $attr['weight'] = 0;

      // pass the array to the import_stage method
      import_stage($attr);
    }

    // set this variable to true to eliminate restaging the data
    variable_set('import_role_staged', TRUE);
  }
}
function import_role_import_process($data) {
  if ($data->type == 'role') {
    db_set_active('old');

    // get the old users data
    db_set_active('old');
    $sql = "SELECT rid, name from role where rid = %d";
    $sql_insert = "INSERT into role(rid, name) values(%d,'%s')";
    $role = db_fetch_object(db_query($sql, $data->impid));
    db_set_active('defualt');
    $status = FALSE;
    if ($role->rid > 0) {
      $status = db_query($sql_insert, $role->rid, $role->name);
    }
    if ($status == FALSE) {
      import_fail($data, 'Role Import failed' . print_r($role, true));
    }
    else {
      import_pass($data, 'Role Import failed' . print_r($role, true));
    }
  }
}

Functions

Namesort descending Description
import_role_import_process
import_role_import_stage hook_import_stage()