1

Using Drupal 7 with Address Field module. How do I get the US state (i.e., the administrative_area) to show as a standard two letter abbreviation rather than the full state name?

What shows now is:

123 Main St
Somewhere, Florida 12345

I want the node to show:

123 Main St
Somewhere, FL 12345

UPDATE

In address.inc in the addressfield module files, there is a function addressfield_format_address_generate which defines an '#options' array of all the different states (an associative array with 2-letter state abbreviations as the keys and state names as the values).

In addressfield.module, there is a function _addressfield_render_address which I believe checks if there is an '#options' defined and if so, tries to get the value for the given key.

So it would seem if I want to change this behavior I somehow need to override _addressfield_render_address

2

As part of this commit, U.S. states (and other countries' administrative areas) will now use their two letter abbreviations when rendered in address formats. Additionally, we now properly provide a comma between the city and state.

Additional code to get this format will no longer be required as of Address Field 7.x-1.0-beta4.

1

One way to do this is

  • Go to the address field and then "Rewrite Results".
  • Select "rewrite the output of this field" checkbox.
  • There in the text area provided, you can construct the result you want using "Replacement Patterns". The pattern for state is [your_field_name-administrative_area]. The patterns for all the fields will be available by clicking "Replacement Patterns"

This will be required only for a few countries for the states are defined in address.inc

0

Addressfield can be extended with ctools based plugins.

However, rather than a writing a whole addressfield plugin, it is worth trying a simpler hook_ctools_plugin_post_alter

That way it may be possible to preserve the default plugin definition and adjust the render callback only.

For example:

function EXAMPLE_ctools_plugin_post_alter(&$plugin, &$info) {
  // Override a function defined by the plugin.
  if ($info['type'] == 'address') {
    $plugin['render callback'] = 'EXAMPLE_format_address_render';
  }
}

Clone, as a base for the render function _addressfield_render_address(&$format, $address) {}

I hope that helps!

0

I have shared some snippets for formatting addressfields on the new DropBucket site. One of them formats states as two letter abbreviations:

Show US State as a 2 letter abbreviation: http://dropbucket.org/node/60
Show only street address and city: http://dropbucket.org/node/229

0

In Drupal 8, showing the abbreviation of the administrative area (in my case for a state in the Unite States)in views can be done using a similar approach to what Varshith described above:

  1. Under the field list, click "Add"
  2. Add the entire address field, not just the administrative area.
  3. For documentation purposes, you will likely want to add the label of "state" to this field to remind yourself later what you are displaying.
  4. Open the "Rewrite Results" section.
  5. Check the "Override the output of this field with custom text" box.
  6. Scroll down and expand the "Replacement patterns" section.
  7. Find the token that ends with "== Raw administrative_area." It should look {{ __administrative_area }}
  8. Copy the token into Text area directly underneath the "Override the output of this field with custom text" check box.
  9. Click Apply to save the field.

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.