Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleaner formatting for addressfield (plus RDFa microdata)
// Override addressfield_tokens themeing function
// Requires addressfield_tokens module as well as addressfield
// Add to template.php and change THEMENAME to name of your theme, then clear cache
function THEMENAME_addressfield_formatter__components($vars) {
$loc = $vars['address'];
$out = '';
// Note added RDFa microdata - https://schema.org/PostalAddress
// Organization name if present (nothing in first or last name fields)
if (!empty($loc['organisation_name']) && $vars['organisation_name']) {
$out = '<span property="name" class="organization-name">' . $loc['organisation_name'] . '</span><br/>';
}
// Street and additional on one line (no space), then break
if (!empty($loc['thoroughfare'])) {
$out .= '<span property="streetAddress" class="street">' . $loc['thoroughfare'] . '</span>';
// Add additional to line only if present (TODO: PO Box?)
if (!empty($loc['premise']) && $vars['premise']) {
$out .= '<span class="additional">' . $loc['premise'] . '</span>';
}
$out .= '</br>';
}
// City, state ZIP
if (!empty($loc['locality'])) {
$out .= '<span property="addressLocality" class="city">' . $loc['locality'] . "</span>, ";
}
if (!empty($loc['administrative_area'])) {
$out .= '<span property="addressRegion" class="state-province">' . $loc['administrative_area'] . '</span>';
}
if (!empty($loc['postal_code'])) {
$out .= ' <span property="postalCode" class="postal-code">' . $loc['postal_code'] . '</span><br/>';
}
// Country, if not US - see l. 121 of addressfield_tokens.theme.inc
if(!empty($loc['country']) && $loc['country'] != 'us') {
$out .= '<span property="addressCountry" class="country">' . _addressfield_tokens_country($loc['country']);
}
$output = '<span vocab="http://Schema.org" typeof="PostalAddress" class="addressfield">' . $out . '</span>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.