3

The Address Field module automatically converts administrative area (i.e., State/Province) abbreviations to full names on node display. I need to do this in my own custom module but can't find the function that the Address Field module uses to do this. How can it be done?

  • drupal.stackexchange.com/a/34163/5147 This answer on a related question may give you some hints... – Patrick Kenny Jun 15 '12 at 16:16
  • Thanks for the link to that (I answered that one!), the problem is that something like country_get_list() doesn't exist for state/provinces, but something like that is exactly what I'm looking for! – Feature Jun 15 '12 at 16:24
  • It seems like this should be a hook or at least a standalone function in addressbook.module but it doesn't look like it is. Might end up being a feature request. – Feature Jun 17 '12 at 12:39
  • The Location Taxonomize module also has a function that does this at line 109 of location_taxonomize_af.module. I don't understand what's going on there but it may help in finding the solution. – Patrick Kenny Jun 20 '12 at 4:48
  • 1
    @PatrickKenny Have a look at the addressfield_format_address_generate() function in plugins/format/address.inc. The abbreviation -> full name mapping seems to be hard coded into that function – Clive Jun 20 '12 at 9:50
4
+50

As mentioned earlier, there's no function in addressfield that does this, it's hard coded and not re-usable. Instead, there's a function in addressfield_tokens module which DOES do this.

Using this function isn't really good practice because it's not an official hook (so it could change at anytime without notice), but as long as you're aware of what's going on and you use the proper checks that the module exists, it's the most flexible way to make this happen. Here's how to use it:

First, ensure that addressfield_tokens.module is installed. Then you can use this function from line 338 in the .module file:

_addressfield_tokens_state($country, $state)

You need the $country because abbreviations are sometimes the same for different countries. So, if we wanted to find the full name of California, we would do this:

$california_fullname = _addressfield_tokens_state('US', 'CA')
return $california_fullname;
  • Can this works with Facets ? – darol100 Feb 25 '15 at 4:33
1

For someone who wants to convert the country name as well, use:

_addressfield_tokens_country($country)
1

Address field contains a very useful function:

addressfield_get_administrative_areas($country);

This returns the administrative areas for the country specified, keyed by the abbreviations.

e.g.

$australian_states = addressfield_get_administrative_areas('AU');

This would result in the following array:

$australian_states = array(
   'ACT' => t('Australian Capital Territory'),
   'NSW' => t('New South Wales'),
   'NT' => t('Northern Territory'),
   'QLD' => t('Queensland'),
   'SA' => t('South Australia'),
   'TAS' => t('Tasmania'),
   'VIC' => t('Victoria'),
   'WA' => t('Western Australia'),
);
  • This is the function that you need to use with newer versions of Address Field. The function _addressfield_tokens_state mentioned in the accepted answer is no longer available. – thirdender Aug 10 '16 at 22:24
  • Also, it looks like you need to call module_load_include('inc', 'addressfield', 'addressfield.administrative_areas'); when addressfield_get_administrative_areas isn't defined. – thirdender Aug 10 '16 at 22:28
0

Take a look at example/plugins/format/addressfield-example.inc. This file handles all the formatting changes. These look like they're hardcoded in.

Take a look at plugins/format/address.inc as well.

0

Drupal 8,

For Country List

$full_country_list = \Drupal::service('country_manager')->getList();

State or Administrative_area list

$full_state_list = \Drupal::service('address.subdivision_repository')->getList(['US']);

Hope, it will be helpful to someone.

0

Background

I needed to output the Province code as the full Province name as well. I dug into the module and in addressfield.address_formats.inc I found the following comment:

- render_administrative_area_value: True if the value should be rendered

I also found the following in address.inc:

'#render_option_value' => $address_format['render_administrative_area_value'],

From this, it appears that, if you retrieve the render array for the addressfield for a given entity, you can simply set the administrative_area sub-field's #render_option_value attribute to TRUE. I did this (code sample below) and, lo and behold, the Addressfield default formatter is observing this attribute nicely. I've got the full province name just as I wanted.

PS: I am making use of entity and uuid_entity modules to store some static content that needs to be version controlled in code and made deployable - thus, instead of node_load($nid) you see entity_load_single_by_uuid('node', $somvar['uuid']); in the code sample below.

Code Sample

$node = entity_load_single_by_uuid('node', $somvar['uuid']);
$address = field_view_value(
  'node',
  $node,
  'field_address_primary',
  $node->field_address_primary[LANGUAGE_NONE][0],
  'default'
);
$address['locality_block']['administrative_area']['#render_option_value'] = TRUE;

return render($address);

Hope this proves useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.