Migrate: Hooks

Last modified: September 23, 2009 - 00:10

Hooks

Migrate hook functions can be created to run custom code during the migration process. They can be created similar to the hook_migrate_init() example below, where the "examplemodule" would be the name of your module. See drupal hook documentation for more information on hooks.

<?php

/**
* Implementation of hook_migrate_init().
* parameters: none
* return: none
*/
function examplemodule_migrate_init() {
 
// do something the first time migrate_invoke_all() is called.
 
  //return an array of settings
 
$settings = array(
   
//add  a new destination
   
'destination' => array('examplemodule_destination' => t('My Destination'))
  );

}
?>

General Structure

The function migrate_invoke_all($kind, $hook, $type = NULL) is a general function that runs all module implementations of a particular hook and then returns an array of results from all those calls. Migrate hook functions have the general structure:
hook_migrate_KIND_HOOK()
or
hook_migrate_KIND_HOOK_TYPE

User Destination Hooks

Note: "destination" has been stripped from hook and function names, for more information see - #552928 - Strip "destination" from hook and function names

hook_migrate_destination_fields_user($type)

returns an array of fields, which can be different on the user type (user role).
params: $type is the locatl role id (rid:int) of the role chosen for "Destination content type:" in the content set admin area.
returns: an array of fields used in the user destination

Example:

<?php
function migrate_migrate_destination_fields_user($type) {
 
// @TODO: Note hard-coded profile node type...
 
$userfields = array(
   
// Note user field is 'name', but this gets overridden from migrate_fields by the author
    // name for a node
   
'username' => t('User: Username'),
   
'pass' => t('User: Password'),
   
'mail' => t('User: Email address'),
   
'created' => t('User: Account created'),
   
'access' => t('User: Account last accessed'),
   
'login' => t('User: Last logged in'),
  );
 
// We're only interested in the CCK fields in this context
 
$profile_fields = migrate_invoke_all('destination', 'fields', 'node', 'profile');
 
  foreach (
$profile_fields as $key => $value) {
    if (!
preg_match('/^field_/', $key)) {
      unset(
$profile_fields[$key]);
    } else {
     
$profile_fields[$key] = t('Profile: ').$value;
    }
  }
 
$fields = array_merge($userfields, $profile_fields);
  return
$fields;
}

?>

hook_destination_delete_user($uid)

params: $uid is the uid of the user being deleted
return: none

Example:

<?php
function migrate_migrate_destination_delete_user($uid) {
 
user_delete(array(), $uid);
}
?>

hook_migrate_destination_import_user($tblinfo, $row)

the main user import hook that runs code necessary to save import data in $row to a destination. The main implementation is migrate_migrate_destination_import_user($tblinfo, $row)
params:

  • $tblinfo contains table information, like field names
  • $row an array of row values for user fields.

return: none

Example:

<?php
function migrate_migrate_destination_import_user($tblinfo, $row) {
 
$sourcekey = $tblinfo->sourcekey;
 
// Begin building user object...
 
$newuser = array();
 
$newuser['status'] = 1;
 
$newuser['roles'][$tblinfo->desttype] = $tblinfo->desttype;
 
// ...and content_profile node object
  // @TODO: Generalize
 
$node = new StdClass;
 
$node->type = 'profile';

  foreach (
$tblinfo->fields as $destfield => $values) {
    if (
$values['srcfield'] && $row->$values['srcfield']) {
     
$newvalue = $row->$values['srcfield'];
    } else {
     
$newvalue = $values['default_value'];
    }
    if (
preg_match('/^field_/', $destfield)) {
     
$node->$destfield = $newvalue;
    } else {
     
$newuser[$destfield] = $newvalue;
    }
 
// ... code omitted: call hook_migrate_destination_prepare_user
 
 
$account = user_save(NULL, $newuser);

// ... code omitted: see actual module code for advanced usage...
}
?>

Since the imported data is a user, user_save() is called to save the user data. hook_migrate_destination_prepare_user() is called right before user_save() giving hooks a chance to change the user object before saving. Note: current implementation includes a lot of extra stuff like content_profile integration that was omitted in this example.

hook_migrate_destination_prepare_user(&$user, $tblinfo, $row)

[need more info]

hook_migrate_destination_complete_user(&$user, $tblinfo, $row)

Proposed
[need more info]

Node Destination Hooks

hook_migrate_destination_complete_node(&$node, $tblinfo, $row)

[need more info]

Miscellaneous Hooks

hook_migrate_global($type, $globals)

[need more info]

hook_migrate_init()

params: none
return: array of settings (destinations)

Example:

<?php
function migrate_migrate_init() {
 
$path = drupal_get_path('module', 'migrate') . '/migrate_destinations.inc';
  include_once(
$path);
  return array(
   
'destination' => array(
     
'comment' => t('Comment'),
     
'node' => t('Node'),
     
'user' => t('User'),
    ),
  );
}
?>

hook_migrate_destination_types()

returns what types are available for each destination.

params: none
return: types - an array in the form array( '[destination]' => array('[type_id]' =>'[type_name]'))

Example:

<?php


function migrate_migrate_destination_types() {
  static
$nodetypes = NULL;

  if (!isset(
$nodetypes)) {
   
$nodetypes = array();
    foreach (
node_get_types() as $type => $info) {
     
$nodetypes[$type] = $info->name;
    }
   
asort($nodetypes);
  }

 
$types = array(
   
'comment' => array('comment' => t('Comment')),
   
'node' => $nodetypes,
   
'user' => user_roles(TRUE),
  );
  return
$types;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.