Is there any way to abbreviate the output of the state fields? In other words, make Rhode Island display as RI or Maine display as ME?

Comments

Anonymous’s picture

Okay, so I got it done the dirty way by commenting out ln 91-161 in plugins/format/address.inc (containing the text that changes US State codes to full words), but I'd really like to figure out a way to get this into an option for the output format. Seems like it would be a useful feature.

userofdrupal’s picture

+1 I also want this.

general_ludd’s picture

This would be quite welcome along with the ability to add a comma between the locality and administrative division (city & state).

nadavoid’s picture

rszrama’s picture

Title: Abbreviate US States » [API change] Make expanding an administrative area code to its full options list value optional
Category: feature » task
Status: Active » Fixed

I'm going to reframe this issue to say the problem isn't abbreviating the value so much as it is not replacing it with its full value on render. As _addressfield_render_address() was written, it was simply replacing the administrative_area value with its full options list value any time the element array had an #options list. This means we could've just updated address.inc to only include the #options array for the form $context['mode'], but I still don't like "automagic" functionality. We should be able to set an options list without it forcing the replacement.

Accordingly, I've added a #render_option_value property to address element arrays that will now be explicitly checked before replacement happens. If you set that property to TRUE in an address element array, you're explicitly saying "render the corresponding value from the options list instead of the raw value." For the countries we already had supported in address.inc, I've set this property to TRUE where it seemed to make sense. Effectively this means we'll now see the state / province abbreviations for the U.S., Canada, and Australia.

This is a bit of a breaking change for folks who have written custom plugins or patched address.inc in the past, meaning people may suddenly see the administrative area code instead of the full name. As such, I'll make sure it gets special attention in the release notes and added [API change] to the issue title.

As part of this issue, I also added comma suffixes to the render mode for U.S. / Canadian addresses.

Commit: http://drupalcode.org/project/addressfield.git/commitdiff/b8c944e

adam_b’s picture

Glad to see this happening. FWIW, Italy also uses the same two-letter convention for provinces - eg "Cortemilia CN" is the city of Cortemilia, in the province of Cuneo.

rszrama’s picture

Ahh, thanks for the tip. I've changed it, will be present in the next commit.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

thirdender’s picture

Issue summary: View changes

This is a beautiful solution, but it took me way too long to figure out how to use :-p I may just be slow, but I'm hoping this code helps someone in need.

To display a full state name ('administrative area') you have two options, from your theme or in a module. Either works, although I'm led to believe the theme based hook_preprocess_field solution may be preferred. The trick is to set the #render_option_value property to TRUE for the part of the address that should be displayed in full.

From your theme:

function THEME_NAME_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'FIELD_MACHINE_NAME') {
    $variables['items'][0]['locality_block']['administrative_area']['#render_option_value'] = TRUE;
  }
}

From a module:

function MODULE_NAME_field_attach_view_alter(&$output, $context) {
  if ($context['entity_type'] == 'ENTITY_TYPE' && $context['entity']->type == 'ENTITY_BUNDLE') {
    $output['FIELD_MACHINE_NAME'][0]['locality_block']['administrative_area']['#render_option_value'] = TRUE;
  }
}
Anonymous’s picture

In views this is very simple. May you not waste your time as I did...

First under fields click add and select the field: Content: Address - Administrative area (i.e. State / Province).

Second, unselect the field that says: "Display the name of administrative area instead of the code."

If you want to show a comma between town and state and show them on one line in views
0. add in the two fields address locality (town) and address admin area (state)
1. go to format, settings
2. make the column name of the state the same as the one above it (the town)
3. in the separator field on the town line put in a ', ' (that's a comma and a space only)

Done.