empty values for firsts addresses fields in format => empty line in html
generated by theme_addresses function in addresses.inc
// Call token module to replace all tokens to their right value. Also,
// use 'adr' microformat
$format_address = '<dl class="adr">'.
token_replace($format_address, 'addresses_adr', $afields, '!', '') .'</dl> ';
// Besides being the Drupal Address info,
// its possible to be used by hCard and VCard (if a address name was provided)
$format_address = '<div class="vcard">'.
token_replace($format_address, 'addresses_general', $afields, '!', '') .'</div>';
solution:
/**
* Generates HTML for the passed address.
*
* @ingroup themeable
* @param $afields
* Array. A single address with the following fields (generally, but it can have more):
* - 'street' => A string representing the street
* - 'additional' => A string for any additional portion of the street
* - 'city' => A string for the city name
* - 'province' => The standard postal abbreviation for the province
* - 'country' => The two-letter ISO code for the country of the address (REQUIRED)
* - 'postal_code' => The international postal code for the address
*/
function theme_addresses($afields) {
// If all fields are hidden, return ''
if (empty($afields)) {
return '';
}
// Get the proper address format
if (!empty($afields['country'])
and !$format_address = variable_get('addresses_format_'. $afields['country'], '')) {
// Load country specifice code .inc file if it exists.
// For example, if country_code for U.S. == 'us', load 'addresses.us.inc'
include_once drupal_get_path('module', 'addresses') .'/countries/'. $afields['country'] .'.inc';
// If the country has an preset address format
$function = 'addresses_address_format_'. $afields['country'];
if (function_exists($function)) {
$format_address = $function();
variable_set('addresses_format_'. $afields['country'], $format_address);
}
}
// If still no proper address format, use the United States format
if (empty($format_address) and !$format_address = variable_get('addresses_format_default', '')) {
include_once drupal_get_path('module', 'addresses') .'/countries/us.inc';
$format_address = addresses_address_format_us();
variable_set('addresses_format_default', $format_address);
}
// Call token module to replace all tokens to their right value. Also,
// use 'adr' microformat
$format_address = token_replace($format_address, 'addresses_adr', $afields, '!', '');
// Besides being the Drupal Address info,
// its possible to be used by hCard and VCard (if a address name was provided)
$format_address = token_replace($format_address, 'addresses_general', $afields, '!', '');
// Replace the new lines for HTML line breaks AND remove
// all empty lines
$format_address = explode("\n", $format_address);
foreach (array_keys($format_address) as $line) {
$format_address[$line] = trim($format_address[$line]);
if (empty($format_address[$line])) {
unset($format_address[$line]);
}
}
$format_address = implode('<br/>', $format_address);
$format_address = '<dl class="adr">'.
$format_address .'</dl> ';
// Besides being the Drupal Address info,
// its possible to be used by hCard and VCard (if a address name was provided)
$format_address = '<div class="vcard">'.
$format_address.'</div>';
// Add the CCS
drupal_add_css(drupal_get_path('module', 'addresses') .'/addresses.css');
return $format_address;
}
Comments
Comment #1
kdebaas commentedHere is a patch for the above.
Comment #2
kdebaas commentedUploaded new patch that also prevents empty divs and dl' being created when ALL address fields are empty:
i.e.:
is now:
Comment #3
koffer commentedcould be my problem when the user dont fill all the information i get some blank space with
the code i get is something like:
you can see the problem in this page
http://www.freelancedj.com/dj-glorious
it suppose the address field are bottom of all other cck fields, but at top of the panels i get the adreess title code.
Comment #4
zoltán balogh commentedsubscribing
Comment #5
zoltán balogh commentedIt is not enough, if the original $format_address contains commas, or any other punctuation. I think, the best way is before the first token_replace, we make the "empty" format, with preg_replace:
After the token replaces we compare the two trimmed versions. If these are equals, then all tokens was empty, and return with an empty string.
Comment #6
zoltán balogh commentedI attached the patch for this
Comment #7
zoltán balogh commentedOhh, a missing semicolon
Comment #8
codycraven commentedZoltán Balogh,
I take it your patch needs to be applied on top of kdebaas's in number 2?
Comment #9
zoltán balogh commentedUhh, I not really remember, but I am afraid, this is independent.
Comment #10
AlexisWilke commentedNote that the comment in the code says that it will change the "\n" with "<br />" but it does not. It puts "\n" back in. This patch would solve that at least.
The only other thing to change, I think, would be the CSS to make everything inline. Otherwise, most address formatting just doesn't work. However, that changes the look of the address output quite a bit.
Also, replacing the "\n" with a <br /> is not generating proper XML because the <br /> will then appear between the </dd> and next <dd> tags, which is illegal (even in proper HTML.)
Instead, we'd need a specific class that removes the inline for specific <dd> tags. Although this sounds like a hack, I think it would work as expected.
Thank you.
Alexis