2

So I'm using this territories plugin to display a list of countries in a dropdown. The issue I'm having is showing these countries within my front-end entry form.

I've used this example but it doesn't work:

{% for option in entry.territoryFieldHandle.options %}
    <li {% if option.selected %}class="active"{% endif %}>{{ option }}</li>
{% endfor %}

Here's my whole form. The other dropdowns work fine.

             {% set jobTypeDropdown = craft.fields.getFieldbyHandle('jobType') %}
        <label for="fields[jobType]">Job Type</label>
        <select name="fields[jobType]">
            {% for option in jobTypeDropdown.settings.options %}
                <option value="{{ option.value }}">{{ option.label }}</option>
            {% endfor %}
        </select>

        {% set jobCategoryDropdown = craft.fields.getFieldbyHandle('jobCategory') %}
        <label for="fields[jobCategory]">Job Category</label>
        <select name="fields[jobCategory]">
            {% for option in jobCategoryDropdown.settings.options %}
                <option value="{{ option.value }}">{{ option.label }}</option>
            {% endfor %}
        </select>

        <label for="fields[country]">Country</label>
        <select name="fields[country]">
            {% for option in entry.country.options %}
                <option value="{{ option }}">{{ option }}</option>
            {% endfor %}
        </select>
  • Have you tried contacting the author? – Brad Bell Nov 3 '14 at 17:23
  • Do you have an {{ entry }} variable available in the template scope? When you say it doesn't work, is it throwing an error or simply not rendering? – John D Wells Nov 5 '14 at 13:09
11

This is not the answer you were looking for, but if you decide to just go with a normal drop-down, then you can paste the following into the dropdown field in the database. This would go in the craft_fields table, yourCountryField row, settings column. After editing the list the way you need it I would suggest removing all line-breaks using grep find/replace ('\n', ''), but it may not matter either.

{"options":[
{"label":"Afghanistan","value":"AF","default":""},
{"label":"Aland Islands","value":"AX","default":""},
{"label":"Albania","value":"AL","default":""},
{"label":"Algeria","value":"DZ","default":""},
{"label":"American Samoa","value":"AS","default":""},
{"label":"Andorra","value":"AD","default":""},
{"label":"Angola","value":"AO","default":""},
{"label":"Anguilla","value":"AI","default":""},
{"label":"Antarctica","value":"AQ","default":""},
{"label":"Antigua and Barbuda","value":"AG","default":""},
{"label":"Argentina","value":"AR","default":""},
{"label":"Armenia","value":"AM","default":""},
{"label":"Aruba","value":"AW","default":""},
{"label":"Australia","value":"AU","default":""},
{"label":"Austria","value":"AT","default":""},
{"label":"Azerbaijan","value":"AZ","default":""},
{"label":"Bahamas","value":"BS","default":""},
{"label":"Bahrain","value":"BH","default":""},
{"label":"Bangladesh","value":"BD","default":""},
{"label":"Barbados","value":"BB","default":""},
{"label":"Belarus","value":"BY","default":""},
{"label":"Belgium","value":"BE","default":""},
{"label":"Belize","value":"BZ","default":""},
{"label":"Benin","value":"BJ","default":""},
{"label":"Bermuda","value":"BM","default":""},
{"label":"Bhutan","value":"BT","default":""},
{"label":"Bolivia","value":"BO","default":""},
{"label":"Bosnia and Herzegovina","value":"BA","default":""},
{"label":"Botswana","value":"BW","default":""},
{"label":"Bouvet Island","value":"BV","default":""},
{"label":"Brazil","value":"BR","default":""},
{"label":"British Indian Ocean Territory","value":"IO","default":""},
{"label":"Brunei Darussalam","value":"BN","default":""},
{"label":"Bulgaria","value":"BG","default":""},
{"label":"Burkina Faso","value":"BF","default":""},
{"label":"Burundi","value":"BI","default":""},
{"label":"Cambodia","value":"KH","default":""},
{"label":"Cameroon","value":"CM","default":""},
{"label":"Canada","value":"CA","default":""},
{"label":"Cape Verde","value":"CV","default":""},
{"label":"Cayman Islands","value":"KY","default":""},
{"label":"Central African Republic","value":"CF","default":""},
{"label":"Chad","value":"TD","default":""},
{"label":"Chile","value":"CL","default":""},
{"label":"China","value":"CN","default":""},
{"label":"Christmas Island","value":"CX","default":""},
{"label":"Cocos (Keeling) Islands","value":"CC","default":""},
{"label":"Colombia","value":"CO","default":""},
{"label":"Comoros","value":"KM","default":""},
{"label":"Congo","value":"CG","default":""},
{"label":"Congo, The Democratic Republic of the","value":"CD","default":""},
{"label":"Cook Islands","value":"CK","default":""},
{"label":"Costa Rica","value":"CR","default":""},
{"label":"Cote D'Ivoire","value":"CI","default":""},
{"label":"Croatia","value":"HR","default":""},
{"label":"Cuba","value":"CU","default":""},
{"label":"Cyprus","value":"CY","default":""},
{"label":"Czech Republic","value":"CZ","default":""},
{"label":"Denmark","value":"DK","default":""},
{"label":"Djibouti","value":"DJ","default":""},
{"label":"Dominica","value":"DM","default":""},
{"label":"Dominican Republic","value":"DO","default":""},
{"label":"Ecuador","value":"EC","default":""},
{"label":"Egypt","value":"EG","default":""},
{"label":"El Salvador","value":"SV","default":""},
{"label":"Equatorial Guinea","value":"GQ","default":""},
{"label":"Eritrea","value":"ER","default":""},
{"label":"Estonia","value":"EE","default":""},
{"label":"Ethiopia","value":"ET","default":""},
{"label":"Falkland Islands (Malvinas)","value":"FK","default":""},
{"label":"Faroe Islands","value":"FO","default":""},
{"label":"Fiji","value":"FJ","default":""},
{"label":"Finland","value":"FI","default":""},
{"label":"France","value":"FR","default":""},
{"label":"French Guiana","value":"GF","default":""},
{"label":"French Polynesia","value":"PF","default":""},
{"label":"French Southern Territories","value":"TF","default":""},
{"label":"Gabon","value":"GA","default":""},
{"label":"Gambia","value":"GM","default":""},
{"label":"Georgia","value":"GE","default":""},
{"label":"Germany","value":"DE","default":""},
{"label":"Ghana","value":"GH","default":""},
{"label":"Gibraltar","value":"GI","default":""},
{"label":"Greece","value":"GR","default":""},
{"label":"Greenland","value":"GL","default":""},
{"label":"Grenada","value":"GD","default":""},
{"label":"Guadeloupe","value":"GP","default":""},
{"label":"Guam","value":"GU","default":""},
{"label":"Guatemala","value":"GT","default":""},
{"label":"Guernsey","value":"GG","default":""},
{"label":"Guinea","value":"GN","default":""},
{"label":"Guinea-Bissau","value":"GW","default":""},
{"label":"Guyana","value":"GY","default":""},
{"label":"Haiti","value":"HT","default":""},
{"label":"Heard Island and Mcdonald Islands","value":"HM","default":""},
{"label":"Holy See (Vatican City State)","value":"VA","default":""},
{"label":"Honduras","value":"HN","default":""},
{"label":"Hong Kong","value":"HK","default":""},
{"label":"Hungary","value":"HU","default":""},
{"label":"Iceland","value":"IS","default":""},
{"label":"India","value":"IN","default":""},
{"label":"Indonesia","value":"ID","default":""},
{"label":"Iran, Islamic Republic Of","value":"IR","default":""},
{"label":"Iraq","value":"IQ","default":""},
{"label":"Ireland","value":"IE","default":""},
{"label":"Isle of Man","value":"IM","default":""},
{"label":"Israel","value":"IL","default":""},
{"label":"Italy","value":"IT","default":""},
{"label":"Jamaica","value":"JM","default":""},
{"label":"Japan","value":"JP","default":""},
{"label":"Jersey","value":"JE","default":""},
{"label":"Jordan","value":"JO","default":""},
{"label":"Kazakhstan","value":"KZ","default":""},
{"label":"Kenya","value":"KE","default":""},
{"label":"Kiribati","value":"KI","default":""},
{"label":"Korea, Democratic People's Republic of","value":"KP","default":""},
{"label":"Korea, Republic of","value":"KR","default":""},
{"label":"Kuwait","value":"KW","default":""},
{"label":"Kyrgyzstan","value":"KG","default":""},
{"label":"Lao People's Democratic Republic","value":"LA","default":""},
{"label":"Latvia","value":"LV","default":""},
{"label":"Lebanon","value":"LB","default":""},
{"label":"Lesotho","value":"LS","default":""},
{"label":"Liberia","value":"LR","default":""},
{"label":"Libyan Arab Jamahiriya","value":"LY","default":""},
{"label":"Liechtenstein","value":"LI","default":""},
{"label":"Lithuania","value":"LT","default":""},
{"label":"Luxembourg","value":"LU","default":""},
{"label":"Macao","value":"MO","default":""},
{"label":"Macedonia, The Former Yugoslav Republic of","value":"MK","default":""},
{"label":"Madagascar","value":"MG","default":""},
{"label":"Malawi","value":"MW","default":""},
{"label":"Malaysia","value":"MY","default":""},
{"label":"Maldives","value":"MV","default":""},
{"label":"Mali","value":"ML","default":""},
{"label":"Malta","value":"MT","default":""},
{"label":"Marshall Islands","value":"MH","default":""},
{"label":"Martinique","value":"MQ","default":""},
{"label":"Mauritania","value":"MR","default":""},
{"label":"Mauritius","value":"MU","default":""},
{"label":"Mayotte","value":"YT","default":""},
{"label":"Mexico","value":"MX","default":""},
{"label":"Micronesia, Federated States of","value":"FM","default":""},
{"label":"Moldova, Republic of","value":"MD","default":""},
{"label":"Monaco","value":"MC","default":""},
{"label":"Mongolia","value":"MN","default":""},
{"label":"Montserrat","value":"MS","default":""},
{"label":"Morocco","value":"MA","default":""},
{"label":"Mozambique","value":"MZ","default":""},
{"label":"Myanmar","value":"MM","default":""},
{"label":"Namibia","value":"NA","default":""},
{"label":"Nauru","value":"NR","default":""},
{"label":"Nepal","value":"NP","default":""},
{"label":"Netherlands","value":"NL","default":""},
{"label":"Netherlands Antilles","value":"AN","default":""},
{"label":"New Caledonia","value":"NC","default":""},
{"label":"New Zealand","value":"NZ","default":""},
{"label":"Nicaragua","value":"NI","default":""},
{"label":"Niger","value":"NE","default":""},
{"label":"Nigeria","value":"NG","default":""},
{"label":"Niue","value":"NU","default":""},
{"label":"Norfolk Island","value":"NF","default":""},
{"label":"Northern Mariana Islands","value":"MP","default":""},
{"label":"Norway","value":"NO","default":""},
{"label":"Oman","value":"OM","default":""},
{"label":"Pakistan","value":"PK","default":""},
{"label":"Palau","value":"PW","default":""},
{"label":"Palestinian Territory, Occupied","value":"PS","default":""},
{"label":"Panama","value":"PA","default":""},
{"label":"Papua New Guinea","value":"PG","default":""},
{"label":"Paraguay","value":"PY","default":""},
{"label":"Peru","value":"PE","default":""},
{"label":"Philippines","value":"PH","default":""},
{"label":"Pitcairn","value":"PN","default":""},
{"label":"Poland","value":"PL","default":""},
{"label":"Portugal","value":"PT","default":""},
{"label":"Puerto Rico","value":"PR","default":""},
{"label":"Qatar","value":"QA","default":""},
{"label":"Reunion","value":"RE","default":""},
{"label":"Romania","value":"RO","default":""},
{"label":"Russian Federation","value":"RU","default":""},
{"label":"Rwanda","value":"RW","default":""},
{"label":"Saint Helena","value":"SH","default":""},
{"label":"Saint Kitts and Nevis","value":"KN","default":""},
{"label":"Saint Lucia","value":"LC","default":""},
{"label":"Saint Pierre and Miquelon","value":"PM","default":""},
{"label":"Saint Vincent and the Grenadines","value":"VC","default":""},
{"label":"Samoa","value":"WS","default":""},
{"label":"San Marino","value":"SM","default":""},
{"label":"Sao Tome and Principe","value":"ST","default":""},
{"label":"Saudi Arabia","value":"SA","default":""},
{"label":"Senegal","value":"SN","default":""},
{"label":"Serbia and Montenegro","value":"CS","default":""},
{"label":"Seychelles","value":"SC","default":""},
{"label":"Sierra Leone","value":"SL","default":""},
{"label":"Singapore","value":"SG","default":""},
{"label":"Slovakia","value":"SK","default":""},
{"label":"Slovenia","value":"SI","default":""},
{"label":"Solomon Islands","value":"SB","default":""},
{"label":"Somalia","value":"SO","default":""},
{"label":"South Africa","value":"ZA","default":""},
{"label":"South Georgia and the South Sandwich Islands","value":"GS","default":""},
{"label":"Spain","value":"ES","default":""},
{"label":"Sri Lanka","value":"LK","default":""},
{"label":"Sudan","value":"SD","default":""},
{"label":"Suriname","value":"SR","default":""},
{"label":"Svalbard and Jan Mayen","value":"SJ","default":""},
{"label":"Swaziland","value":"SZ","default":""},
{"label":"Sweden","value":"SE","default":""},
{"label":"Switzerland","value":"CH","default":""},
{"label":"Syrian Arab Republic","value":"SY","default":""},
{"label":"Taiwan, Province of China","value":"TW","default":""},
{"label":"Tajikistan","value":"TJ","default":""},
{"label":"Tanzania, United Republic of","value":"TZ","default":""},
{"label":"Thailand","value":"TH","default":""},
{"label":"Timor-Leste","value":"TL","default":""},
{"label":"Togo","value":"TG","default":""},
{"label":"Tokelau","value":"TK","default":""},
{"label":"Tonga","value":"TO","default":""},
{"label":"Trinidad and Tobago","value":"TT","default":""},
{"label":"Tunisia","value":"TN","default":""},
{"label":"Turkey","value":"TR","default":""},
{"label":"Turkmenistan","value":"TM","default":""},
{"label":"Turks and Caicos Islands","value":"TC","default":""},
{"label":"Tuvalu","value":"TV","default":""},
{"label":"Uganda","value":"UG","default":""},
{"label":"Ukraine","value":"UA","default":""},
{"label":"United Arab Emirates","value":"AE","default":""},
{"label":"United Kingdom","value":"GB","default":""},
{"label":"United States","value":"US","default":""},
{"label":"United States Minor Outlying Islands","value":"UM","default":""},
{"label":"Uruguay","value":"UY","default":""},
{"label":"Uzbekistan","value":"UZ","default":""},
{"label":"Vanuatu","value":"VU","default":""},
{"label":"Venezuela","value":"VE","default":""},
{"label":"Viet Nam","value":"VN","default":""},
{"label":"Virgin Islands, British","value":"VG","default":""},
{"label":"Virgin Islands, U.S.","value":"VI","default":""},
{"label":"Wallis and Futuna","value":"WF","default":""},
{"label":"Western Sahara","value":"EH","default":""},
{"label":"Yemen","value":"YE","default":""},
{"label":"Zambia","value":"ZM","default":""},
{"label":"Zimbabwe","value":"ZW","default":""}
]}
|improve this answer|||||
  • This is just what I was looking for :) thanks, ps working without removing line brakes – Davor Jan 16 '16 at 19:25
  • Glad it worked @Davor. And thanks for the follow up on line breaks. – Douglas McDonald Jan 17 '16 at 21:26
  • Ha, one of those times when you think 'It's a longshot but I'll just Google this before subjecting myself to this tedium...' So glad I did... – James Smith Mar 19 '18 at 15:52
4

Unfortunately the drop down options in Territories Dropdown Plugin are not automatically available in templates. It need to be accessed through a field on an element. You're currently trying to access it through 'entry' which i assume doesn't exist in your template (as you would have copied this part).

eg. I'm using it on projects in my template:

{% for option in project.country.options %}
<option {% if option.selected %}selected="selected"{% endif %} value="{{ option }}">{{ option.label }}</option>
{% endfor %}

For me 'project' is an entry in my section 'projects' which has been declared above and i have given it the field 'country' of type 'Territories - Dropdown' in the admin panel.


If you want to access the options with out an instance of an entry you'll still need to create a field with the type 'Territories - Dropdown'. Then replace 'country' in the code below with the handle you give your field.

{% for option in craft.fields.getFieldByHandle('country').getFieldType.options %}
<option value="{{ option }}">{{ option.label }}</option>
{% endfor %}
|improve this answer|||||
  • Many thanks Jered for the example. I found that outputting {{ option }} caused an array to string conversion error and I needed to use {{ option.value }} instead of just {{ option }} to get this working. – Tidy Feb 1 '17 at 12:15
4

This doesn't really address the problem either, but for anyone looking for "States" data (to replicate what Douglas McDonald did), here it is:

{"options":[{"label":"Alabama","value":"AL","default":""},{"label":"Alaska","value":"AK","default":""},{"label":"Arizona","value":"AZ","default":""},{"label":"Arkansas","value":"AR","default":""},{"label":"California","value":"CA","default":""},{"label":"Colorado","value":"CO","default":""},{"label":"Connecticut","value":"CT","default":""},{"label":"Delaware","value":"DE","default":""},{"label":"District of Columbia","value":"DC","default":""},{"label":"Florida","value":"FL","default":""},{"label":"Georgia","value":"GA","default":""},{"label":"Hawaii","value":"HI","default":""},{"label":"Idaho","value":"ID","default":""},{"label":"Illinois","value":"IL","default":""},{"label":"Indiana","value":"IN","default":""},{"label":"Iowa","value":"IA","default":""},{"label":"Kansas","value":"KS","default":""},{"label":"Kentucky","value":"KY","default":""},{"label":"Louisiana","value":"LA","default":""},{"label":"Maine","value":"ME","default":""},{"label":"Maryland","value":"MD","default":""},{"label":"Massachusetts","value":"MA","default":""},{"label":"Michigan","value":"MI","default":""},{"label":"Minnesota","value":"MN","default":""},{"label":"Mississippi","value":"MS","default":""},{"label":"Missouri","value":"MO","default":""},{"label":"Montana","value":"MT","default":""},{"label":"Nebraska","value":"NE","default":""},{"label":"Nevada","value":"NV","default":""},{"label":"New Hampshire","value":"NH","default":""},{"label":"New Jersey","value":"NJ","default":""},{"label":"New Mexico","value":"NM","default":""},{"label":"New York","value":"NY","default":""},{"label":"North Carolina","value":"NC","default":""},{"label":"North Dakota","value":"ND","default":""},{"label":"Ohio","value":"OH","default":""},{"label":"Oklahoma","value":"OK","default":""},{"label":"Oregon","value":"OR","default":""},{"label":"Pennsylvania","value":"PA","default":""},{"label":"Rhode Island","value":"RI","default":""},{"label":"South Carolina","value":"SC","default":""},{"label":"South Dakota","value":"SD","default":""},{"label":"Tennessee","value":"TN","default":""},{"label":"Texas","value":"TX","default":""},{"label":"Utah","value":"UT","default":""},{"label":"Vermont","value":"VT","default":""},{"label":"Virginia","value":"VA","default":""},{"label":"Washington","value":"WA","default":""},{"label":"West Virginia","value":"WV","default":""},{"label":"Wisconsin","value":"WI","default":""},{"label":"Wyoming","value":"WY","default":""}]}
  1. Create a dropdown field.
  2. Navigate to the craft_fields table
  3. Locate the row with your states handle
  4. Add the data to the settings column
  5. Output states in a dropdown

    {% for state in craft.fields.getFieldByHandle('states').getFieldType.options %}
        <option value="{{ state.value }}">{{ state.label }}</option>
    {% endfor %}
    

Even better, create a reusable macro:


{% macro stateDropdownOptions(currentState) %}
    <option>Select One</option>
    {% for state in craft.fields.getFieldByHandle('states').getFieldType.options %}
        <option value="{{ state.value }}"{{ currentState is defined and state.value == currentState ? ' selected' : '' }}>
            {{ state.label }}
        </option>
    {% endfor %}
{% endmacro %}

and use it:


{% import 'macros/form' as formMacros %}

...

<select name="states">
    {{ formMacros.stateDropdownOptions(row.state) }}
</select>

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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