For example when an address with the form
[Street]
[City], [Province]is being displayed and all of those fields are empty, addresses 1.06 will display just a single line containing a comma. Since the US address uses this form and ends up being the default format for a lot of cases, this would seem to be a common problem.
I've written a patch to fix this. It should handle all of the address formats distributed with version 1.06.
I have very little time for thorough testing right now, but it does pass preliminary testing on US addresses.
In particular it will not display any non-token text at the beginning and end of the address format, however none of the current formats use this.
The code rewrites the second part of addresses.inc's theme_address function -- meaning that if you want to use it just for your theme without patching the addresses module, that's an option. IE backup your copy of addresses.inc, apply the patch, copy the resulting theme_addresses over to your theme's template.php and rename it yourtheme_addresses. Then restore your backup. If there is a preferred way to post alternative theme functions, please let me know & accept my apologies.
It accesses the token API at a lower level in order to
- split the address into chunks of tokens and delimiters
- tokenize the tokens in the token chunks (ie convert them into the actual address data
- concatenate the resulting set of address parts and delimiters using "delimiter precedence". This means that as it moves over empty fields (ie address fields whose tokenized value is an empty string) it remembers the highest-precedence delimiter since it encountered a field with a value. It uses the highest-precedence delimiter thus encountered when it eventually reaches another address.
Newlines have the highest precedence, followed by delimiters that contain non-whitespace, followed by whitespace. This actually ends up being about the closest thing to the right way to present addresses that are missing fields.
So for example if someone has given a city but no province, you don't end up seeing a dangling comma at the end of the line; if there would be an empty line, this method sucks it up. (Note that empty lines are fixed as of 1.06 anyway.)
Really this is a hack, and something much more structured should be done if this module is really intended to be used to display incomplete addresses. But it does seem to get the job done for now at least. If it does end up getting integrated into the module, it should probably be optional, and preferably with a way to enable it for specific country formats.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | theme-addresses-delimiter_precedence-0.2.patch | 5.75 KB | intuited |
| theme-addresses-delimiter_precedence.patch | 4.56 KB | intuited |
Comments
Comment #1
intuited commentedJust a minor note: If you're going to patch the function and copy it into your template, you'll need to copy the support function _addresses_delimiter combine as well; this function will be patched in above theme_addresses. Sorry if that was misleading..
Comment #2
intuited commentedHmm.. trouble: sometimes (I guess at certain paths), token_get_values returns a pair of empty arrays. This patch (to replace the previous one) fixes that issue.
Comment #3
intuited commented...and by "fixes", I mean "avoids". I haven't yet come across a path that causes that issue that actually displays an address, so it's this far been okay just to return the untokenized format string. This problem seems to happen on node submission. I'm hoping that the problem will become more clear sometime soon; definitely would appreciate any insight folks have to lend. Anyway that change should keep it out of trouble, at least.
Comment #4
codycraven commentedintuited,
Thank you for your efforts with this patch, this is something that I have been wanting to tackle since becoming a maintainer of the project. I previously opened another issue that incorporates the functionality that you are working on #680354: Unify hCard/plain formats to singular format.
If you can review the goals in that issue and possibly incorporate the plan for bullet one "Parsing system" of http://drupal.org/node/680354#comment-2457068 in to your patch, I would love to have a release in Addresses 1.x that fixes that element and prepares it for the other goals in #680354: Unify hCard/plain formats to singular format.
To note, I am not sure if this will be possible with the existing theming hooks and thus I slated this fix for 2.x.
Comment #5
codycraven commentedComment #6
sarahwbs commentedI'm having this problem as well, though I think under slightly different circumstances. What I'm seeing is if a city is specified, along with a postal code, but no province, the city gets displayed with a trailing comma. Even with the patch in place, I'm still seeing this.