Take the 2-minute tour ×
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
add comment

4 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
add comment

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
add comment

This is actually pretty simple, once you know the hooks and have some example code. You're correct that you need to use the ctools plugin api. So in a custom module, add:

/**
 * Implements hook_ctools_plugin_directory().
 */
function mymodule_ctools_plugin_directory($module, $plugin) {
  if ($module == 'addressfield') {
    return 'plugins/addressfield/' . $plugin;
  }
}

Then you drop plugin files in the directory you specified, within your custom module. In this case, plugins/addressfield. A file that will do what you're looking for would contain something like this:

<?php
/**
 * @file
 * city-comma.inc
 *
 * Adding comma after city.
 */

/**
 * Plugin definition for address field format.
 */
$plugin = array(
  'title' => t('Comma after city.'),
  'format callback' => 'mymodule_city_comma',
  'type' => 'address',
  'weight' => -80,
);

/**
 * Callback for plugin definition.
 */
function mymodule_city_comma(&$format, $address, $context = array()) {
  if (in_array($address['country'], array('US', 'CA'))) {
    if ($context['mode'] == 'render') {
      // Add comma after city.
      $format['locality_block']['locality']['#suffix'] = ',';
    }
  }
}

This file would actually go in a directory called 'formats' inside the directory defined in the hook. So: mymodule/plugins/formats/city-comma.inc.

share|improve this answer
add comment

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
add comment

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.