This is a quick tip on Drupal 7 views and formatting an address field. Address field has only one widget with a standard formatter. When selected in a view, it will deliver the whole address in one pre-defined html block. There are many reasons, however, why we would like to output the different parts of an address in a view. Theming is but one example, but maybe you want to build a csv data export of addresses, or put the different parts in a sortable table, or you want to display the country only and leave all the other stuff out.
This is a good example of a very simple and basic functionality that is very well hidden. I didn't find much documentation on how to do it so I hope I can save you some time with this little write up.
- Step 1. Let's build a new content view, or open the view you are working on that contains a content type with an address field available. We will show fields in this view.
- Step 2. Go ahead and add the address field to the view. You will notice there is only one possibility in the list of fields, and no address components (like Country, Locality, etc.) in this step. Click apply.
- Step 3. Proceed to the address settings. Again, no apparent formatting options here that are very useful for our goals.
- Step 4. Turning on the 'Rewrite the output of this field' checkbox will make a textbox available for our rewrites, and offer 'Replacement pattern' suggestions. This is yet another hidden fieldset that will finaly reveal all the treasures we were looking for: all the raw address data we could wish for.
- Step 5. Just copy any address component and put it in the rewrite textbox. It is important to leave the pattern between brackets exactly as suggested. You can put the field in a div for theming purposes, or just leave it like it is to output the raw data.
What we are looking for is inside the even surprising 'Rewrite Results' fieldset. Open the fieldset.
For every component of the address you want to output in your view follow steps 2 to 5 untill you got all you need.
Step 6. Save the view. Check it out. Tweak when necessary. Rejoice.