How do you create a theme function for overriding the formatting of an Address Field in Drupal 7? I didn't want to create a CTools plugin or work with render arrays.
1 Answer
- Download & enable the Addressfield Tokens module (https://www.drupal.org/project/addressfield_tokens). This will provide additional tokens and theme functions for Addressfield.
- Set the field in question to display using the "Address components" format.
- Select all the fields you wish to display. Note that the separator does not matter, unless you choose to use it in your theme function.
- Create a theme function named
THEMENAME_addressfield_formatter__components
, whereTHEMENAME
is the name of your theme. Here is an example I created in my template.php to add microdata for XHTML+RDFa: https://gist.github.com/evandonovan/d47a3874e26151c4c07f6c20b7edc204. It is designed especially with the United States in mind, so may not work as well on other countries. - Clear the theme registry for your theme function to take effect. If you have Drush, you can use
drush cc theme-registry
. Otherwise, see https://www.drupal.org/node/173880#theme-registry for other means to clear.