Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedOkay, 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.
Comment #2
userofdrupal CreditAttribution: userofdrupal commented+1 I also want this.
Comment #3
general_ludd CreditAttribution: general_ludd commentedThis would be quite welcome along with the ability to add a comma between the locality and administrative division (city & state).
Comment #4
nadavoid CreditAttribution: nadavoid commentedThere is more information over in #1413858: How to control address display formatting?
Comment #5
rszrama CreditAttribution: rszrama commentedI'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
Comment #6
adam_b CreditAttribution: adam_b commentedGlad 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.
Comment #7
rszrama CreditAttribution: rszrama commentedAhh, thanks for the tip. I've changed it, will be present in the next commit.
Comment #9
thirdender CreditAttribution: thirdender commentedThis 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:
From a module:
Comment #10
Anonymous (not verified) CreditAttribution: Anonymous commentedIn 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.