2

I modified the "Address form (country-specific)" bundled with the Addressfield module to take only a country, administrative area, and city; this works fine on the node edit form, but when the field is displayed upon viewing the node, it looks like this:

addressfield display output

What I want to do is display it on a single line with commas like this:

Indianapolis, Indiana, United States

I am using the Views module to display the field. How can I rewrite (using Views or custom code) the addressfield output to be a single line?

7

I had to do the same thing a couple of days ago, this is what I came up with using hook_field_formatter_info() and hook_field_formatter_view():

function MYMODULE_field_formatter_info() {
  return array(
    'inline_address_field' => array(
      'label' => t('Inline Address Field'),
      'field types' => array('addressfield'),
    )
  );
}


function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $build = array();

  $count = 1;
  foreach ($items as $item) {
    $inline_address = "<h3>Address $count</h3>";

    $address_parts = array();
    if (!empty($item['first_name'])) {
      $address_parts[] = $item['first_name'];
    }
    if (!empty($item['last_name'])) {
      $address_parts[] = $item['last_name'];
    }
    if (!empty($item['premise'])) {
      $address_parts[] = $item['premise'];
    }
    if (!empty($item['thoroughfare'])) {
      $address_parts[] = $item['thoroughfare'];
    }
    // And so on for all of the columns that you're interested in, in the order
    // that you want them to be displayed.

    $inline_address .= '<p>' . implode(', ', $address_parts) . '</p>';

    $build[] = array('#markup' => $inline_address);
    $count++;
  }

  return $build;
}

It provides a field formatter you can pick for the address field on the node's "Manage Display" form.

2

This can also be done with the Location Taxonomize module.

Enable Location Taxonomize and create a taxonomy term reference as described in the module's README. Then, in Views, display this term reference field instead of the addressfield.

The benefit of this approach is that the module's configuration page allows you to easily choose if you would like to display the country/administrative area abbreviations or the actual names.

  • Good find, that's a very handy looking module – Clive Jun 20 '12 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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