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

rclations’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.

overflowing’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

There is more information over in #1413858: How to control address display formatting?

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:

<?php
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:

<?php
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;
  }
}
?>