PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Using a dropdown for Address field and adding dropdown predefined lists

  1. kyle
    Member

    This is a bit of a two-parter.

    1) I'd like to replace the address field with a dropdown of provinces - is this possible using hooks?

    2) Is it possible to add your own predefined lists to the dropdown using hooks? Right now you have items like Continents, Gender, and Age. I'd like one that is more site-specific (list of vehicle models) so the client doesn't have to paste in the items on their own.

    Posted 14 years ago on Thursday December 2, 2010 | Permalink
  2. Yes, you can accomplish both of these things using hooks. The new documentation site will have documentation for these hooks when it launches with the public release of Gravity Forms v1.5. For now, I have included links to PDF's for each of the hooks.

    To add a custom address type and customize the state/region/province drop down you use the gform_address_types hook. Download gform_address_types PDF

    To add a new predefined list to the available predefined lists in the bulk add you would use the gform_predefined_choices hook. Download gform_predefined_choices PDF

    Posted 14 years ago on Thursday December 2, 2010 | Permalink
  3. kyle
    Member

    Thanks Carl,

    Figured I'd post my results here for anyone who might be interested:

    First of all, I'm a fool - because once again I've discovered that Gravity Forms is one step ahead of me (and I've only had it for about 24 hours). I can set the Address Type in the Address field to "Canadian" and it already handles all of this. Anyhow, for anyone who wants to do it for their own country, here's how you'd do it for Canada (if it wasn't already built in):

    add_filter("gform_address_types", "canadian_address");
    
    function canadian_address($address_types, $form_id) {
    
        $address_types["canada"] = array(
            "label" => "Canadian",
            "country" => "Canada",
            "zip_label" => "Postal Code",
            "state_label" => "Province",
            "states" => array(
                "Alberta",
                "British Columbia",
                "Manitoba",
                "New Brunswick",
                "Newfoundland & Labrador",
                "Northwest Territories",
                "Nova Scotia",
                "Nunavut",
                "Ontario",
                "Prince Edward Island",
                "Quebec",
                "Saskatchewan",
                "Yukon")
        );
        return $address_types;
    
    }

    and for adding a predefined list:

    add_filter("gform_predefined_choices", "add_predefined_choices_honda_models");
    
    function add_predefined_choices_honda_models($choices) {
    
        $choices["Honda Models"] = array(
            "Accord Coupe",
            "Accord Crosstour",
            "Accord Sedan",
            "Civic Coupe",
            "Civic Sedan",
            "CR-V",
            "CR-Z",
            "Element",
            "Fit",
            "Insight",
            "Odyssey",
            "Pilot",
            "Prelude",
            "Ridgeline",
            "S2000"
        );
        return $choices;
    }

    So I guess this leads to a new question - is it possible to set the default state and/or address type? That way whenever I add an Address field it gets set to Address Type "Canadian" and default province "Ontario".

    Posted 14 years ago on Friday December 3, 2010 | Permalink