I'm running a migration to move volunteers from a csv file to drupal. I have written the following code.

    * @file
    * Migration for Volunteer File 

ini_set('auto_detect_line_endings', TRUE);

class VolunteerMigration extends Migration {

public function __construct($arguments) {

    $this->description = t('Imports Volunteers from CSV file ');

    // Create a source object
    $columns = array(
        0 => array('pkid', 'PKID'),
        1 => array('fname', 'First Name'),
        2 => array('lname', 'Last Name'),
        3 => array('email','Email'),
        5 => array('dayphone','DayPhone'),
        6 => array('eveningphone','EveningPhone'),
        7 => array('address','Address'),
        8 => array('city', 'City'),
        9 => array('state','State'),
        10 => array('postalcode','zip'),
        11 => array('occupation','Occupation'),
        12 => array('interests','Interests'),
        13 => array('created','Timestamp'),
    $address_arguments = array(
        'first_name' => array('source_field' => 'fname'),
        'last_name' => array('source_field' => 'lname'),
        'thoroughfare' => array('source_field' => 'address'),
        'locality' => array('source_field' => 'city'),
        'administrative_area' => array('source_field'=>'state'),
        'postal_code' => array('source_field' => 'postalcode'),
    $this->source = new MigrateSourceCSV('/mnt/www/cylindrical/theoregonproject.com/data/drvollist.csv', $columns, array('header_rows' => 1));

    // Create a destination object
    $this->destination = new MigrateDestinationNode('volunteers');

    // Create a map
    $this->map = new MigrateSQLMap(
            'pkid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,

    // Create Mappings
    $this->addFieldMapping('title', NULL)->DefaultValue('Volunteer');
    $this->addFieldMapping('field_volunteer_id', 'pkid');
    $this->addFieldMapping('field_email', 'email');
    $this->addFieldMapping('field_dayphone', 'dayphone');
    $this->addFieldMapping('field_eveningphone', 'eveningphone');
    $this->addFieldMapping('field_occupation', 'occupation');
    $this->addFieldMapping('field_interests', 'interests')


public function prepareRow($row) {

    if ( $row->zip ) { 
        $row->city = zipcity($row->zip);

public function zipcity($zip) {
    $ch = curl_init('http://ziptasticapi.com/' . $zip);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $result = curl_exec($ch);

    $json = json_decode($result,true);

    return $json['city'];

I'm am using the ziptastic api to fill in the cities using the zipcode. Running the migration returns all except the address portion of the migration. The zipcity function works as expected (returns city given a zipcode) but none of the address arguments are migrated.

I have adapted this code from a previous and successful migration minus the preparerow / zipcity additions.



Found the issue. When using the addressfield module, you should have field mappings that allow for preparerow to work. I thought that the address_arguments array was enough to create the field maps. Also I was using the csv column title 'Zip' instead of the the mapped field title 'postalcode'

Under my address field mapping I added the following code.

    $this->addFieldMapping(NULL, 'fname');
    $this->addFieldMapping(NULL, 'lname');
    $this->addFieldMapping(NULL, 'address');
    $this->addFieldMapping(NULL, 'city');
    $this->addFieldMapping(NULL, 'state');
    $this->addFieldMapping(NULL, 'postalcode'); 

I also moved the function zipcity to my helper function file to allow other migrations to use it if needed.

This post https://drupal.org/node/1326044 was key into figuring out the missing link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.