Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add whole countries select list in grav forms #187

Closed
geogeorgejoseph opened this issue Sep 6, 2017 · 1 comment
Closed

How to add whole countries select list in grav forms #187

geogeorgejoseph opened this issue Sep 6, 2017 · 1 comment
Labels

Comments

@geogeorgejoseph
Copy link

How to add whole countries select list in grav forms.

@rhukster
Copy link
Member

rhukster commented Sep 6, 2017

This is quite simple really, but does require a 'tiny' bit of code. You can put this code in your current 'theme' or in a custom plugin if you wish.

I'll use the Antimatter theme as an example here: In your themes' php file add this method:

<?php
namespace Grav\Theme;

use Grav\Common\Theme;

class Antimatter extends Theme
{
    public static function countryCodes()
    {
        return array (
            'AF' => 'Afghanistan',
            'AX' => 'Åland Islands',
            'AL' => 'Albania',
            'DZ' => 'Algeria',
            'AS' => 'American Samoa',
            'AD' => 'Andorra',
            'AO' => 'Angola',
            'AI' => 'Anguilla',
            'AQ' => 'Antarctica',
            'AG' => 'Antigua & Barbuda',
            'AR' => 'Argentina',
            'AM' => 'Armenia',
            'AW' => 'Aruba',
            'AC' => 'Ascension Island',
            'AU' => 'Australia',
            'AT' => 'Austria',
            'AZ' => 'Azerbaijan',
            'BS' => 'Bahamas',
            'BH' => 'Bahrain',
            'BD' => 'Bangladesh',
            'BB' => 'Barbados',
            'BY' => 'Belarus',
            'BE' => 'Belgium',
            'BZ' => 'Belize',
            'BJ' => 'Benin',
            'BM' => 'Bermuda',
            'BT' => 'Bhutan',
        );
    }
}

I've trimmed this down for easy viewing but you can copy/paste the full country list from https://github.com/umpirsky/country-list/blob/master/data/en_US/country.php

This resource has other translations too.

Then all you need to do is reference this static function in your form definition:

    country:
      type: select
      label: Country
      data-options@: '\Grav\Theme\Antimatter::countryCodes'

2017-09-06 at 11 16 am

here it is in the admin, but should work the same in the frontend too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants