Welcome back to the Drupal Advent Calendar for another door opening, and it’s day 7. Seven days? A whole week! I’m frankly quite amazed that it’s lasted this long!
Inside today’s door nestles the Address module. On the surface this is a very simple module, but it hides a fair bit of complexity.
One of the fantastic things about Drupal is the field system. This allows you to add new fields to entities, which is a catch all for content types and other items containing fields on the site. This includes content types built into Drupal core, ones you define yourself, and ones defined by modules you install. With all of them you can add your own fields.
There are a wide range of fields provided by Drupal. These allow you to add many different fields to your content, including plain text strings and longer formatted text, values selected from lists, and media like images.
But the real power of the field system is that you can install modules that provide new field types, and the Address module is just such a module.
As the name suggests, it provides fields for dealing with real world addresses.
You could, of course, just create individual fields for the address, but there are some problems with this. First of all, if you want to make the country field a selectable list, you would need to maintain the list of country names, and track when countries change names. You probably would also find it difficult to account for all the differing ways that addresses are composed and formatted around the world.
The Address field takes care of all of that for you. It knows how to format addresses correctly for over 200 countries. It not only has a well maintained list of country names, but for most countries it lists internal regions. For example, when I select Ireland, it displays the correct address format, and includes a selectable list of Irish counties. And it knows that in Ireland we refer to postal codes as Eircodes.
In the above example, I didn’t want to share my own address. Sure, I barely know you. And I couldn’t find a definitive address for Santa Claus. But the address of the President of Ireland is public knowledge, so that will do nicely.
If you don’t need a full address, the module also provides a couple of additional fields.
The country field is fairly self explanatory. It provides just a list of countries to select from.
The zone field is a little more complex. By default, it lets you select a country and an area within the country, be it a US state, a Canadian province, or an Irish county. However, it can be configured to restrict to a specific subset.
I think the Address module is a great example of the power of the Drupal field system, and a must have if you want to collect postal addresses on your site.
I hope that you’ve enjoyed the first week of the Advent Calendar, and that you’ll come back tomorrow for a module that will further enhance the Address field.
Comments