Project:Addresses
Version:6.x-1.x-dev
Component:User interface
Category:bug report
Priority:critical
Assigned:Unassigned
Status:needs review

Issue Summary

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

#1

Status:active» needs review

Here is a patch for the above.

AttachmentSize
theme_addresses.patch 2.7 KB

#2

Uploaded new patch that also prevents empty divs and dl' being created when ALL address fields are empty:

i.e.:

<?php
    $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>';
?>

is now:
<?php
 
if (!empty($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>';
  }
?>
AttachmentSize
theme_addresses_#2.patch 2.75 KB

#3

could be my problem when the user dont fill all the information i get some blank space with

and
in panels with node profile module

the code i get is something like:

<dt>Address</dt>
<dd><div class="vcard"><dl class="adr"><br/></dl>  </div></dd>
<p id="content-profile-view">  <h3 class="content-profile-title" id="content-profile-title-dj">

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.

#4

subscribing

#5

It 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:

$empty_format = preg_replace('/\[\S*\]/' , '', $format_address);

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.
if (trim($empty_format) == trim($format_address)) {
  return '';
}

#6

Version:6.x-1.05» 6.x-1.x-dev

I attached the patch for this

AttachmentSize
theme-addresses.patch 1.04 KB

#7

Ohh, a missing semicolon

AttachmentSize
theme-addresses.patch 1.04 KB

#8

Zoltán Balogh,

I take it your patch needs to be applied on top of kdebaas's in number 2?

#9

Uhh, I not really remember, but I am afraid, this is independent.

#10

Note 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