Drupal - Location Province name / code

Kevin's picture

[b]Q:[/b] How can I get the drupal [url=http://drupal.org/project/location]location[/url] module to display proper province/state/county names rather than just its code (e.g. Cork vs CK)?

[b]A:[/b] One quite easy way of achieving this is to override the default location display template, [i]location.tpl.php[/i] and get it to output the full province name.

The location module provides a means of storing location information with your drupal content. It does all sorts of great things like integrating with [url=http://drupal.org/project/views]views[/url] and the google maps ([url=http://drupal.org/project/gmap]gmap[/url]) module, however some aspects of its behaviour can be a bit awkward and unwieldy splurging location information in such a way as to play absolute havoc with your web design!

One such issue is that, by default, location always displays provinces using a short abbreviation code rather than the full province name. This makes sense in the US and Canada where a state's name is often abbreviated, California becomes CA etc. However in Ireland, the UK and in many other countries such abbreviations are never used and would make no sense at all to the hapless viewer!

The problem is discussed here:

http://drupal.org/node/115323

and here:

http://drupal.org/node/134881

Various solutions are suggested, some involve hacking the location module's code - this is not a good idea at all and can't be recommended! Most agree that a good way to handle the problem is through theming - by overriding the default location display template and so changing how the location information (and hence the province) is displayed.

The default template is in a file called [i]location.tpl.php[/i] in the [i]sites/all/modules/location[/i] directory. Now, instead of changing this file you should just override it for your theme by copying it to your theme's folder and changing it there. Once the file has been copied it is very important to let drupal know that it use this file instead by refreshing the 'theme registry'. One way of doing this is to navigate to Administer > Site configuration > Performance, and click on the 'Clear cached data' button.

Now how do we change the template to display full province names? Looking at the template we can see the lines of code where it adds the province to the output here:


if ($province) {
$city_province_postal[] = ''. $province .'';
}

In this code the [i]$province[/i] variable represents the province code and not its name. Luckily we can use a function called [i]location_province_name()[/i] to get the full name from this code as follows:


if ($province) {
$city_province_postal[] = ', '. location_province_name($country, $province) .'';
}

This should do the trick - at least it did for me! While you're editing the template you might like to change other aspects of location display, but this should be enough to get rid of thise annoying abbreviations - thanks to all at drupal.org for the info!

Let us know if this works for you or if you have any other suggestions!

Share this post: 

Recent Work

Some of Ireland's biggest brands use Pride Design to Design, Build, Manage, Market and Maintain their websites.

Website design for St.Patricks Credit Union in Dublin, Including Loan Calculators.

Website design and development for a step by step wedding and other stationary ordering website f

New website designed and developed for Cork's newest café, which is based in Merchants Quay in Co

New Website design for Pain Relief clinic based in Cork City.

Website for a busy Dublin city centre pub.

We redrafted the Ballymaloe house website to modernise the look and feel of the site and to also

We revamped the website for the Ballymaloe Cookery School.

We revamped the Cully & Sully website onto Drupal.

Rachel Allen was brought up in Dublin and left home at eighteen to study at the world-famous Ball

Clóna Dairy Products are a Dairy Company based in Clonakilty, Co. Cork, since 1919.

Project Arts Centre is Ireland’s leading centre for the presentation and development of c