Last updated March 21, 2010. Created by ultimike on July 17, 2009.
Edited by zserno. Log in to edit this page.

Here's an example of using the Migrate module's API to import User pictures. The code below imports the a user picture filename from an external table and adds it to Drupal's core user picture (as seen on a "user/[uid]/edit" page). Please note that this doesn't import user pictures to a Content Profile node.

I'm working on importing a site from XOOPS to Drupal, so this code should work pretty much as-is for anyone doing the same thing. The incoming data is coming from the "xoops_users" table. The "user_avatar" field has data that looks like this:

user_avatar
- - - - - - -
blank.gif
blank.gif
cavt4302f80005c2e.jpg
blank.gif

I created a custom module called "xoops", created a xoops.info file, then a xoops.module file with the following code:

<?php
// $Id$
/*
* Implementation of hook_migrate_destination_fields_user()
*
* Define the new field(s) that you want to import.
*/
function xoops_migrate_destination_fields_user($type) {
 
$fields = array();
 
$user_fields['picture'] = 'User: Picture';
 
$fields = array_merge($fields, $user_fields);
  return
$fields;
}
/*
* Implementation of hook_migrate_destination_prepare_user()
*
* Add the new fields to the user array
*/
function xoops_migrate_destination_prepare_user(&$account, $tblinfo, $row) {
 
// Grab the value of each field to import or set it to the default.
 
foreach ($tblinfo->fields as $destfield => $values) {
    if (
$values['srcfield'] && isset($row->$values['srcfield'])) {
     
$newvalue = $row->$values['srcfield'];
    } else {
     
$newvalue = $values['default_value'];
    }
   
/* This is specific to my particular instance. In the old CMS, the default user
     *   picture was set to "blank.gif". Change those to Drupal's default user picture or
     *   add the imported file name (and new path) to the user array.
     *
     * Don't forget to manually copy the files from the old CMS to Drupal!
     */
   
if ($destfield == 'picture') {
      if (
$newvalue == 'blank.gif') {
       
$account['picture'] = variable_get('user_picture_default', '');
      }
      else {
       
$account['picture'] = file_create_path(variable_get('user_picture_path', 'pictures')) .'/xoops/'. $newvalue;
      }
    }
  }
  return
$errors;
}
?>

Once the module is enabled, a "User: Picture" destination field will be available on the content set mapping page. Select your incoming field ("user_avatar", in my case) as the "source field" and it should be good to go.

I used the July 17, 2009 version of the Migrate module as checked out from CVS.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.