3

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.

4 Answers 4

2

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.

1

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.

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

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 '';
  }
}
?>
0

You're correct that Addressfield uses Ctools plugins.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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