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 1

  1. Download & enable the Addressfield Tokens module (https://www.drupal.org/project/addressfield_tokens). This will provide additional tokens and theme functions for Addressfield.
  2. Set the field in question to display using the "Address components" format.
  3. 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.
  4. Create a theme function named THEMENAME_addressfield_formatter__components, where THEMENAME 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.
  5. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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