Created
January 10, 2019 03:54
-
-
Save evandonovan/d47a3874e26151c4c07f6c20b7edc204 to your computer and use it in GitHub Desktop.
Cleaner formatting for addressfield (plus RDFa microdata)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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