Address fieldtype for Expression Engine

June 28, 2011 at 3:22am.

This entry is about Expression Engine

33 comments.

While setting up the field groups for a new Expression Engine-based project (which I’m very excited to tell you about, just as soon as it’s ready!), I got to thinking: instead of creating a bunch of separate fields for an address—address, city, state, zip code, country—there should be a way to drop them all in as one field. A quick check of Devot-EE didn’t turn up such a thing, so I threw one together this afternoon. VZ Address is a fieldtype for EE2 that combines the standard address fields you’ve probably set up many times into one unit. It even gives you a variety of ways to get the address data back out, and it works as a cell in a Matrix field.

VZ Address

Template Tages

Single Tags
{address_field [style="microformat|schema|rdfa|plain|inline"]}

Will output the complete address, in a standard format. Use the style attribute to get code that supports one of the markup standards for parseable content (defaults to microformats) or to get the address with no html markup at all (plain or inline).


{address_field:street}
{address_field:street_2}
{address_field:city}
{address_field:region}
{address_field:postal_code}
{address_field:country [code="yes"]}

Output particular pieces of the address. If you use the parameter code=“yes” on the country tag, you will get the the international country code rather than the full country name.

{address_field:map_url [source="google|yahoo|bing|mapquest"] [params=""]}

Output a URL to the address on any one of a variety of mapping services. Specify which service you want to use with the source parameter (Google Maps is the default). Anything you put in the params parameter will be added to the end of the map URL, use it to specify zoom levels, map types, etc.

Tag Pair


{address_field}
    {street}
    {street_2}
    {city}, {region} {postal_code}
    {country}
{/address_field}

If you need more control over the output, use the tag pair to output each part of the address individually.

Installation

Download and unzip the archive. Upload the vz_address folder to /system/expressionengine/third_party/.

Visit the VZ Address repo on GitHub

Comments:

Chris Brauckmuller gravatar

Chris Brauckmuller on June 28, 2011 at 4:31pm#1

This looks very nice. Will definitely download and give it a whirl!

Dylan Smith gravatar

Dylan Smith on July 19, 2011 at 2:38am#2

Would love to see a 1.x version of this!

EliVZ gravatar

EliVZ on July 19, 2011 at 2:33pm#3

Chris- Thanks! I hope it worked out for you.

Dylan- I would love to say that there is a 1.x version in the works, but honestly I don’t know when I will have time to port it. You are welcome to give it a shot, if you know some PHP. Or obviously if you or your employer wanted to sponsor the development, we’ll talk…. :)

Adam Kragt gravatar

Adam Kragt on July 22, 2011 at 8:22pm#4

I don’t suppose you have any plans to add geocoding? It would be perfect if it could spit out latitude and longitude as well. :)

EliVZ gravatar

EliVZ on July 23, 2011 at 3:46pm#5

Adam- Yes! That is in the plans, although it will probably be another month or so before I have a chance to get to it (going on vacation). I will definitely keep you posted and send you a beta version once I have it.

Raj gravatar

Raj on August 7, 2011 at 1:10am#6

Can you make the city and states auto populate too ... like you did for the country….fantastic addon…was looking for it since long

EliVZ gravatar

EliVZ on August 7, 2011 at 1:39am#7

Raj- I thought about that. The problem is, I would need some kind of API to provide that data, as I’m not going to compile a list of all the cities in all the states in the world. I haven’t looked very hard for one yet…. maybe someday.

Daniel Matarazzo gravatar

Daniel Matarazzo on October 11, 2011 at 5:59pm#8

Hello, for some reason after installing the latest version of vz_address (as of this writing), I’m getting the letter “a” returned for each field, even if the field was left empty in the cp. For example, a simple call to my field in the template, {practitioner_address style=“plain”} returns this:
a a a, a a a
I tried removing and re-installing the plugin to no avail.
Assuming I can get this fixed, is there any way to search addresses via dynamic_parameters?
Thanks!

EliVZ gravatar

EliVZ on October 11, 2011 at 9:52pm#9

Daniel,

The new version - 1.1.1 - fixes that bug. Good catch!

As for your other request, do you mean using the search:address=”” style searching? It should work as long as you use “contains” matching (the default). The data is stored as a serialized array, which means each part of the address is stored as normal text, but there are random characters between them. So search:address=“New York” would work but search:address=“New York, NY” would not. I don’t think there is any practical way around that caveat.

Dane gravatar

Dane on December 8, 2011 at 7:12am#10

I have a similar question to the last one.  I’m trying to display listings by state, and would like to be able to show, for example, all of the Arizona listings on one page.  The only way I can get it to work is to use: search:address=“AZ” - the problem with that is that it returns results that have, for instance, the word “plaza” in the street address, because of the az in the word.  Unfortunately search:address:region doesn’t work, and neither does search:region.

Any ideas for me?

The only other solution I can think of is to create a category for all 50 states, then go through my hundreds of listings and add them to their respective state categories one by one.

Or I guess I could use an {if} statement inside of the channel entries to filter out anything that doesn’t match the field, but I’m afraid that will be server intensive, since (I believe) it would have to parse every result to match the field.

EliVZ gravatar

EliVZ on December 9, 2011 at 9:50pm#11

Dane,

Unfortunately, modifying the search parameter in that way would require overriding the entire exp:channel:entries tag, which would be impractical (if someone has an idea for a work-around, I’d love to hear it!).

So I think your options are:
1. Use the {if} statement, like you mentioned. You are right that this will have a processing overhead… you would definitely want to cache the template if you do this. Mark Croxton’s Stash and/or Switchee add-ons would be very useful here.
2. The data is stored as a serialized array, which is a fairly easy-to-read format. (You might want to take a look in your exp_channel_data table to see an example.). The values are quoted, so you should be able to do something like search:address=‘“AZ”’ to get what you want. Or even search:address=‘“region”;s:2:“AZ”’ to ensure that you only get values in the “State/Province” field if there is the potential for the same value in tho fields, with “2” being the length of the value.

Neither is ideal, I’m afraid, but they should work. I will keep this problem in mind and update the add-on if I have any flashes of inspiration.

Dane gravatar

Dane on December 9, 2011 at 10:19pm#12

Perfect!  That last solution works exactly like I need it to.  You saved me a lot of time doing tedious categorization.

Aidan gravatar

Aidan on February 29, 2012 at 3:42pm#13

Does this work with Safecracker?  I can see it in the edit entry safecracker form, but it won’t save changes.  ideas?

EliVZ gravatar

EliVZ on March 1, 2012 at 2:07am#14

Aidan,

It works in my testing, but I must admit I have not so far had a reason to use it in a Safecracker form in production, so it’s not something I have tested extensively. Do you have a link to the page so I can see what’s going on? (Feel free to email it to my first name at this domain, if you don’t want the URL out there for everyone to see.)

Neil gravatar

Neil on April 17, 2012 at 10:49am#15

Hi there,

This is an awesome fieldtype. I have a few issues with the latest version from devot-ee.

I added {cf_office_address:map_url [source=“google”]} to my template, but it does not seem to output the Map URL for my {cf_office_address} VZ Address fieldtype.

I’m also note sure how to format the parameters, perhaps you have a few for guidance? :D

Also, I cannot get the {address_field:country [code=“yes”]} to work. It just always outputs the full country name. :/

Grateful for any help with these. Thank you. :D

EliVZ gravatar

EliVZ on April 17, 2012 at 5:06pm#16

Neil,

Sorry, I can see how that would be unclear if you don’t read a lot of PHP documentation. The square brackets just mean that the parameter is optional, you don’t actually include them in your template. So try: {cf_office_address:map_url source=“google”}

Neil gravatar

Neil on April 18, 2012 at 1:55pm#17

Thanks man, just realised as well that the version on Devot-ee is only version 1.2.1 but it has the instructions for the version that is on Github. Updated to that version and the Google map link works now. :)

Christian gravatar

Christian on June 26, 2012 at 7:46am#18

I have setup zoo vistor to allow member profiles being updated. I can display everything using your fieldtype but can’t save.

site is currently local here is a snippet for the street part:

{cf_member_address}
               
                  <label for=“member_firstname”>Address</label><br>
                  <input type=“text” name=“cf_member_address:street” id=“address_field:street” value=”{street}”/>
               
{/cf_member_address}

I assume the name attribute needs to be altered?

Cheers

EliVZ gravatar

EliVZ on June 26, 2012 at 4:52pm#19

Christian,

Is there are reason you can’t use the standard {cf_member_address:field} syntax to output all the Address fields at once? That is what I have done in the past to use VZ Address with Zoo Visitor. If you can’t do that (just need some of the address fields, perhaps) the name attribute should be in the format: cf_member_address[street].

Ruud gravatar

Ruud on November 14, 2012 at 9:00pm#20

How does a map_url from a Low Variable look programatically? I just can’t seem to get it functioning right…

As it is not a standard channel fieldtype tye {address_field:map_url} doe snot work right. Placing {map_url} in the {exp:low_variables:parse var=“address_field_name” multiple=“yes”} doesn’t work either it seems.

Any help would be appreciated :-)

- R -

EliVZ gravatar

EliVZ on November 15, 2012 at 12:59am#21

Ruud- That actually isn’t possible at the moment, because Low Variables doesn’t support the {field_name:function} syntax the way native EE fieldtypes do. I could probably implement it as a tag parameter, and I will consider this a feature request to do so. Not sure when I’ll have time to get to it though… if you know any PHP you could probably figure it out fairly easily.

James Buckley gravatar

James Buckley on January 1, 2013 at 5:55pm#22

Hey there, great addon!

I have a quick question, sorry if it’s obvious.

I have the following code


<a href=”{cf_event_location:map_url [source="params="">

Which spits out the below URL. The issue it that I don't want the NAME included in the url passed to Google. How can I control that? I need the NAME for other ways the data is displayed, but in this case, it's breaking the google maps location search (not finding it). When I remove the name it works as intended.

http://maps.google.com/maps?q=Center+Church+UCC,+11+Center+Street,+Manchester+,+Connecticut,+06040,+US

Thanks sir!

EliVZ gravatar

EliVZ on January 5, 2013 at 1:37am#23

James- Funny, I actually added the name field into the mapping URL in the last version by request. But it seems that - while that improves results when the place name is in Google’s database - it breaks search when it’s not. Version 1.3.3 now leaves the name out by default and I added an include_name parameter for people who want it.

Sam gravatar

Sam on March 25, 2013 at 3:20am#24

Hello EliVZ,

how can i use the country dropdown in safecracker?
I am trying something like this:

<select name=“member_address[country]” id=“member_address[country]”>
  {options:member_address[country]}
  <option value=“member_address[country]{option_value}”{selected}>{country}</option>
      {/options:member_address[country]}
</select>

but it doesnt work.

Thanks for help,

Sam

EliVZ gravatar

EliVZ on March 25, 2013 at 4:55pm#25

Sam,

You have two options:

1. You can output all the Address fields at once (including the country drop-down) using {field:member_address}.

2. If you need more control over the output, you would need to add those country options on your own - there is no helper tag in VZ Address to do that for you. You might try this plugin, which looks to do exactly what you want: http://devot-ee.com/add-ons/multi-language-country-list. (I haven’t personally used it, so no promises.)

Jay gravatar

Jay on April 29, 2013 at 11:03am#26

Hi EliVZ!

Was wondering if you had plans to update the fieldtype for EE 2.6 compatibility? Was mentioned: http://devot-ee.com/add-ons/support/vz-address/viewthread/9356

I’m having some issues getting addresses to actually save ever since I updated to 2.6 (was fine beforehand).

Thank you!

Susan Kitchens gravatar

Susan Kitchens on May 16, 2013 at 2:30pm#27

I’ve been working on a complex form in (profile:edit) that uses vz_address in Safecracker, and would like to note a minor correction to Comment #26 above. More importantly, I’d like to flesh out the tags that worked for me because I love finding more full specific working examples out in the wild, and after the figure-it-out time required for me to come up with this, I’ll add my own to save the next person some time.

These examples show markup for Twitter Bootstrap Form (horizontal form) in Profile:Edit (which is based on Safecracker)

Using vz_address inside a safecracker form inside the custom loop:


{exp:profile:edit return=“profile” class=“form-horizontal”}
...
{custom_fields}
...
<div class=“control-group”>
  <label for=”{field_name}” class=“control-label”>{if required}* {/if}{field_label}</label>
  {if vz_address}
      <div class=“controls”>
      {field_instructions}
      {display_field}
      </div>
  {/if}
...
</div>
{/custom_fields}
...
{/exp:profile:edit}


more in next comment… there’s a character limit I ran up against….

Susan Kitchens gravatar

Susan Kitchens on May 16, 2013 at 2:31pm#28

(continued from previous comment)
Using VZ_address outside of the custom_field loop

What happens if you want to take this outside of profile:edit’s and safecracker’s custom_field loop?

At minimum, you need to use the nesting tags and call the field tags inside it:

{member_vzaddy_address}
  {field:member_vzaddy_address}
{/member_vzaddy_address}

(by the way, EliVZ, you’d reversed the order in comment #26, in reply to Christian on June 26 2012. field: goes in front of the name of your customfield.)

Here’s what it looks like with the addition of label and instructions inside the Twitter Bootstrap form markup:

{exp:profile:edit return=“profile” class=“form-horizontal”}
...
{member_vzaddy_address}
<div class=“control-group”>
  <label class=“control-label”>{label:member_vzaddy_address}</label>
  <div class=“controls”>
      {instructions:member_vzaddy_address}
      {field:member_vzaddy_address}
  </div>
</div>
{/member_vzaddy_address}
...
{/exp:profile:edit}

Now, if you want to exercise more strict control over form markup, you’ll need to follow Christian’s (#25) example above and explicitly code up all the address forms. (Happily, in my case, the item in question is for one geographic locale so we can dispense with the country select item altogether.

Eli, thank you for a wonderful add-on. Please feel free to adapt this for your template tag documentation.

Susan Kitchens gravatar

Susan Kitchens on May 16, 2013 at 2:33pm#29

Cringe. oops. Correction. When I said a correction to comment #26 and #25, I was wrong.

The comment I was offering a correction for was #19.

Citing Christian’s example in #18.

Apologies.

Dallas Hudgens gravatar

Dallas Hudgens on June 3, 2013 at 12:55pm#30

I am also using the address field in a form where users can updated their address information. I was able to use {field:member_address} (member_address being the name of the address field I created in zoo visitor) which displayed all the address fields correctly.

What I am having trouble with is how to display values that are already set. If a user has already entered their address, I’d like it to show up in the form when the user wants to updated it.
Using this tag, I can’t set a value for each field. Does anyone have a suggestion or work around?

Thanks!

EliVZ gravatar

EliVZ on June 13, 2013 at 11:55pm#31

Dallas- Are you saying when you edit an existing entry through SafeCracker, the Address fields don’t remember the data that was previously entered? That should definitely not be the case…. that’s not even something I have to take care of in code, that is part of SafeCracker. Or am I misunderstanding?

Robbiegod gravatar

Robbiegod on June 12, 2014 at 2:59pm#32

Is this plugin v1.4.0 compatible with version 2.7.3 of EE?

I see this message in my develop log:


Deprecated function EE_Fieldtype() called in system/expressionengine/third_party/vz_address/ft.vz_address.php on line 27.
From template tag exp:channel:entries in Site/branches_xml.
Deprecated since 2.6. Use EE_Fieldtype::__construct() instead.

Any option to fix this?

EliVZ gravatar

EliVZ on June 12, 2014 at 11:38pm#33

Robbie- That is actually something that was fixed in 1.4. Could the deprecation message be left over from before you updated? Let me know if not.

Got something to say?