Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm using the addressfield module with Drupal 7.10. I want a comma after the city name when displaying the addressfield. Currently addresses display like:

123 Main St
SomeCity California 90100 

I want it to display like:

123 Main St
SomeCity, California 90100 

(Note the comma after "SomeCity")

How can I achieve this?

UPDATE

This whole formatting of addresses is still shrouded in mystery for me. I'm starting to think the way to control the formatting is to create a custom ctools plugin. I found this api documentation:

http://api.drupalhelp.net/api/addressfield/addressfield.module/group/addressfield_format/7

I'm going to explore it.

share|improve this question

3 Answers

Sure you can, but this needs some PHP skill.

Basically, you're going to override theme function responsble for rendering this field, which is theme_addressfield_container() in this case.

From function's declaration in theme_addressfield_container() you can see that all address attributes are being imploded. You can easily override this function to get the result that you want.

share|improve this answer
Do you mean replacing this line $output .= $element['#children']; with code the creates html strings for the street, city, state, zip as desired? – User Jan 4 '12 at 8:06

Open your theme folder, Put this code in template.php

<?php
// Putting comma between City and State of address field
function YOUR_THEME_NAME_addressfield_container(&$variables) {
  $element = $variables['element'];
  if($element['#title'] == 'City') {
    $element['#children'] = trim($element['#children']) . ',';
  }
  $element['#children'] = trim($element['#children']);
  if (strlen($element['#children']) > 0) {
    $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
    $output .= $element['#children'];
    $output .= '</' . $element['#tag'] . ">";
    return $output;
  }
  else {
    return '';
  }
}
?>
share|improve this answer

You're correct that Addressfield uses Ctools plugins.

I suggest you take a look How does one create a new Ctools plugin (content type, access, etc)? where I show how to create new plugins.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.