4th June 2015

How to Drupal Migrate data into an AddressField

John Ennew
Technical Director

The Drupal Migrate module provides developers with a neat framework for migrating code from a variety of sources into Fields on Entities in Drupal.

The Address Field module provides a handy way of storing address information in a field which can be added to any entity such as a node content type or user.

This guide assumes you know the basics of running a Drupal Migration with Drupal migrate.

The Address Field module stores the parts of its address in separate columns in the database. The Address Field module supports sub fields in Drupal migrate. This means the syntax for mapping source data to destination field is as follows.

This assumes you have an address field called field_address and separate source elements for each of the different parts of the address. If you don't, you'll want to break up the address into this format in the prepareRow function of your migration class.

    $this->addFieldMapping('field_address', 'country');
    $this->addFieldMapping('field_address:thoroughfare', 'thoroughFare');
    $this->addFieldMapping('field_address:premise', 'premise');
    $this->addFieldMapping('field_address:locality', 'locality');
    $this->addFieldMapping('field_address:administrative_area', 'administrativeArea');
    $this->addFieldMapping('field_address:postal_code', 'postCode');

The thing to note in the code above is that the country code is not a sub field and must be mapped first!