Drupal Commerce Blog

What's happening in the world of Drupal Commerce.

Major improvements in addressfield 7.x-1.0-rc1

Many people know that addressfield hasn’t been the easiest module to maintain. There are over 200 countries in the world, each with its own addressing requirements. Addressfield attempted to provide a sane default for all of them, along with a plugin architecture for handling per-country customizations. But with so many countries, the list of needed improvements became never-ending, and the customizations themselves started gathering in only one plugin (address.inc), which quickly became impossible to maintain.

A radical change was needed, so after a lot of research we introduced a new plan for Drupal 8, along with a brand new PHP library we can depend on from addressfield 8.x-2.x. The new plan resolves around two powerful ideas:

  • The introduction of address formats, which hold information on how a country’s address and its form need to be rendered and validated.
  • The use of Google’s addressing dataset, freely available and built for Chrome and Android, with address formats for 200 countries.

The introduced solutions were obviously superior to anything we had before that, but Drupal 8 is still far from production, and we needed improvements on our Drupal 7 sites today, so we decided to try and backport as many concepts as we could into the 7.x-1.x codebase. The result of that is addressfield 7.x-1.0-rc1:

  • Created the concept of an address format.
    An address format is an array which contains the per-country list of used fields, list of required fields, and the field labels.
    Imported address formats for 200 countries, derived from Google’s dataset.
  • Moved administrative areas out of address.inc, into their own include file (addressfield.administrative_areas.inc), added an alter hook.
  • Added administrative areas for 20 countries:
    Argentina, Chile, China, Colombia, Egypt, Hong Kong, India, Indonesia, Ireland, Jamaica, Japan, Malaysia, Mexico, Peru, Russia, South Korea, Spain, Turkey, United Arab Emirates, Venezuela.
  • The field can now be optional.
  • Added plugins for hiding the street address and/or the postal code.

For the first time we know precisely which fields we need, and which labels they should have. We also know which fields are required, instead of just requiring them all, which is a relief to citizens of 130 countries with optional postal codes, for example. Our contributors from all over the world (Russia, China, Australia, others) have already confirmed the big improvements. The best part is, these 50 commits did not result in a single backwards compatibility break.

What’s left? These improvements are still exclusive to the Drupal 8 version:

  1. A “format” string for each address format, showing the exact layout of the fields. The D7 version already has most of this data, derived from Google’s dataset, but it is not as precise.
  2. A more complete list of administrative areas (some still haven’t been imported).
  3. Postal code validation
  4. Predefined localities for Brazil and Chile, localities and dependent localities for China/Taiwan/South Korea.
  5. Switching between major-to-minor and minor-to-major field orderings for China/Japan/South Korea.

The good news is that 1-3 are also backportable, so the community could make it happen.

Please update to 7.x-1.0-rc1 and help us find any remaining issues, so that we can tag 1.0 in the near future.

Tags: 
Bojan Zivanovic
Posted: Dec 15, 2014