The Address Field module provides a Drupal 7 field for standardized storage of postal addresses based on the OASIS xNAL standard.

Address Field provides one or more addresses to an entity, module, or user. It understands the differences in address styles for each country, and offers per-country edit forms and formatting.

For example, for Australia:

City might be the city and, in the case of a big city, is usually the suburb. An address in Wahroonga is usually Wahroonga but might be Wahroonga, Sydney.

We have states: ACT, NSW, NT, QLD, SA, TAS, VIC, and WA. An address in Wahroonga is Wahroonga NSW.

The postal code is usually called postcode and is a four-digit integer with the first digit indicating a state. NSW postcodes start with 2. Sydney is 2000. An address in Wahroonga is Wahroonga NSW 2076.

For a review of the module's inner workings, see the Address Field from a code perspective.

How to use adressfield with tokens / rules see Using tokens with addressfield

Comments

schuffr’s picture

Hi all,

I am having a problem migrating address fields. Everything else in my migration works, but address field simply is blank. I have searched for many hours and havent found a definitive answer that works. I am using Drupal 7.37, Migrate 7.x-2.7, Address Field 7.x-1.1. I have set the default value for address to US and I am using the : subfield notation. Any ideas why this wouldn't work?

many thanks!

My migration class is as follows:

    <?php
    class DOCInfoMigration extends Migration {
      public function __construct($arguments) {
    
        parent::__construct($arguments);
    
        $this->description = t('Loads early profiles data to DOCInfo profile');
            
       	/********* Source *********/
       	// MySQL database as source
        $query = Database::getConnection('default', 'default')
                 ->select('DOCInfo', 'u')
                 ->fields('u', array('uid',
                                     'first',
                                     'last',
                                     'phone',
                                     'phonetype',
                                     'dob',
                                     'gender',
                                     'membertype',
                                     'year',
                                     'make',
                                     'model',
                                     'new_used',
                                     'tshirt_it',
                                     'street',
                                     'apt',
                                     'city',
                                     'state',
                                     'zipcode'));
                 
        $this->source = new MigrateSourceSQL($query);
    
    		$this->destination = new MigrateDestinationProfile2('doc_info'); // use machine name of profile
    
    		/*********** Map **********/
    		// Create a "map" which is used to translate primary keys*/
        $this->map = new MigrateSQLMap($this->machineName,
          array(
            'uid' => array(
              'type' => 'int',
              'alias'=> 'u'
            ),
            ),
          MigrateDestinationProfile2::getKeySchema()      
        );
    
        /*********** Connect DOCInfo to user **********/
        $this->addFieldMapping('uid', 'uid');
    #         ->sourceMigration('DOCInfo')  // If user migration class was named 'MyUserMigration', the string is 'MyUser'
    #         ->description(t('The assignment of DOCInfo source data to the respective DOCInfo fields'));
    
        /******* Field mappings ******/
        $this->addFieldMapping('language')->defaultValue('en');
        $this->addFieldMapping('field_fname','first');
        $this->addFieldMapping('field_fname:language')->defaultValue('en');
        
        $this->addFieldMapping('field_lname','last');
        $this->addFieldMapping('field_lname:language')->defaultValue('en');
    
        $this->addFieldMapping('field_home_phone','phone');
        $this->addFieldMapping('field_home_phone:language')->defaultValue('en');
        $this->addFieldMapping('field_phone_type','phonetype');
    
        $this->addFieldMapping('field_dob','dob');
        $this->addFieldMapping('field_doc_gender','gender');
    
        $this->addFieldMapping('field_doctype','membertype');
        $this->addFieldMapping('field_docbikeyear','year');
        $this->addFieldMapping('field_docmake','make');
        $this->addFieldMapping('field_docmodel','model');
        $this->addFieldMapping('field_docmodel:language')->defaultValue('en');
        $this->addFieldMapping('field_docnewused','new_used');
        $this->addFieldMapping('field_italian_t_shirt','tshirt_it');
    
    
        $this->addFieldMapping('field_address')->defaultValue('US');
        $this->addFieldMapping('field_address:thoroughfare','street');
        $this->addFieldMapping('field_address:premise','apt');
        $this->addFieldMapping('field_address:locality','city');
        $this->addFieldMapping('field_address:administrative_area','state');
        $this->addFieldMapping('field_address:postal_code','zipcode');
    
    
        /*** Unmapped destination fields ***/
        $this->addUnmigratedDestinations(array('revision_uid',
    #                                           'field_address',
                                               'field_address:sub_administrative_area',
                                               'field_address:dependent_locality',
                                               'field_address:sub_premise',
                                               'field_address:organisation_name',
                                               'field_address:name_line',
                                               'field_address:first_name',
                                               'field_address:last_name',
                                               'field_address:data',
                                               'field_dob:timezone',
                                               'field_dob:rrule',
                                               'field_dob:to',));
      }
    } 
peterx’s picture

Hello schuffr, Please open an issue with one of the modules. After the issue is resolved, you can add the solution to this documentation page, if this page needs a change.

tomrenner’s picture

Just a quick explanation how we managed to add Austrian administrative areas with the "new" hooks:

function mymodule_addressfield_address_formats_alter(&$address_formats) {
  $address_formats['AT'] = array(
    'used_fields' => array('locality', 'administrative_area', 'postal_code'),
    'required_fields' => array('locality', 'postal_code'),
    'render_administrative_area_value' => TRUE,
  );
}
function mymodule_addressfield_administrative_areas_alter(&$administrative_areas) {
  $administrative_areas['AT'] = array(
    'B' => t('Burgenland'),
    'W' => t('Vienna'),
    'NO' => t('Lower Austria'),
    'S' => t('Salzburg'),
    'ST' => t('Steiermark'),
    'OO' => t('Upper Austria'),
    'T' => t('Tyrol'),
    'K' => t('Carinthia'),
    'V' => t('Vorarlberg'),
  );
}