Chris 2pha Brown

Chris Brown

Drupal, Javascript, Three.js, 3D

website blog

Drupal Migrate V2 and Addressfield

While trying to migrate data from a legacy CakePHP system to Drupal 7 recently I had a problem getting an Addressfield field to play nicely.
What I had origianly was this (which did not work).

    $this->addFieldMapping('field_customer_address:thoroughfare', 'address');
    $this->addFieldMapping('field_customer_address:locality', 'suburb');
    $this->addFieldMapping('field_customer_address:administrative_area', 'state');
    $this->addFieldMapping('field_customer_address:postal_code', 'postcode');

Looking all over the web I found many examples saying to use the arguments function. eg.

    $address_arguments= array(
      'thoroughfare' => array('source_field' => 'address'),
      'locality' => array('source_field' => 'suburb'),
      'administrative_area' => array('source_field'=>'state'),
      'postal_code' => array('source_field' => 'postcode'),
    );
    $this->addFieldMapping('field_customer_address')
    ->arguments($address_arguments);

But when trying this I got a message from the module saying that this was not correct. I think the above code was/is for version 1 of the migrate module.
It turns out that all I needed to do was to set a default country. ie.

    $this->addFieldMapping('field_customer_address')->defaultValue('AU');
    $this->addFieldMapping('field_customer_address:thoroughfare', 'address');
    $this->addFieldMapping('field_customer_address:locality', 'suburb');
    $this->addFieldMapping('field_customer_address:administrative_area', 'state');
    $this->addFieldMapping('field_customer_address:postal_code', 'postcode');