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.