Currently if the source data lists the country with its full name, import will fail as addressfield's importer requires ISO two-letter country code.

It would be very nice if it automatically checked if the data is greater than 2 letters, and then translated country->code using country_get_list.

Comments

rerooting’s picture

Title: Integrate country_get_list to allow migrating country by name, transparently. » Convert source country as text into ISO with addressfield destination handler
Version: 7.x-2.4 » 7.x-2.x-dev

I have a way to do this that I'm unsure of how to add to the (legacy) destination handler, however I have done something similar with my Migration class:

this is in my Migration class:

public function prepareRow($row) {
   $countryiso = mymodule_migrate_fetch_country_iso($row->Country);
   $row->Country = $countryiso;
   return TRUE;
}

then, in my .module file I added this:

function mymodule_migrate_fetch_country_iso($country){
  if(!$country){
    return FALSE;
  }
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $countries = country_get_list();
  $country_iso = array_search($country, $countries);
  if (!$country_iso) {
    return FALSE;
  }
  else {
    return $country_iso;
  }
}

This converts, for example, a source value of "United States" to 'US' using the same approach that addressfield itself currently uses to expose the full country label in it's field handler. The field destination handler will change with addressfield 2.x's new approach to storing all of this address information, but will continue to work regardless.

I should mention that I did this with migrate 2.6's default field handler and the latest dev of migrate_extras, which doesn't include the addressfield handler at all. This same approach will work with the legacy addressfield handler and 2.5 just fine as well.