Skip to content

Commit

Permalink
Added craft.vzAddress.countries variable containing an array of all…
Browse files Browse the repository at this point in the history
… the countries in the world
  • Loading branch information
elivz committed Jun 15, 2017
1 parent 7c35022 commit 4bab6eb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ Accepts the same parameters as `staticMapUrl`.
* The options array mirrors the [MapOptions](https://developers.google.com/maps/documentation/javascript/3.exp/reference#MapOptions) configuration from Google Maps Javascript API. All available options are shown in the example above. Only the `width`, `height`, & `zoom` options are set by default.
* The icon array is used for configuring the look of the marker icon. All available options are shown in the example above.

### Bonus: Output a list of all countries

VZ Address makes available an array of all countries indexed by their country code as `craft.vzAddress.countries`. You can use this to generate your frontend entry form, or wherever you may need a list of all the countries in the world.

<select name="country">
{% for countryCode, countryName in craft.vzAddress.countries %}
<option value="{{ countryCode }}">{{ countryName }}</option>
{% endfor %}
</select>

Installation
------------

Expand Down
8 changes: 8 additions & 0 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"version": "1.5.0",
"downloadUrl": "https://github.com/elivz/VzAddress-Craft/archive/1.5.0.zip",
"date": "2017-06-14T21:30:00+00:00",
"notes": [
"[Added] `craft.vzAddress.countries` variable containing an array of all the countries in the world."
]
},
{
"version": "1.4.2",
"downloadUrl": "https://github.com/elivz/VzAddress-Craft/archive/1.4.2.zip",
Expand Down
2 changes: 1 addition & 1 deletion vzaddress/VzAddressPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getName() {
}

public function getVersion() {
return '1.4.2';
return '1.5.0';
}

public function getSchemaVersion() {
Expand Down
17 changes: 2 additions & 15 deletions vzaddress/fieldtypes/VzAddressFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
class VzAddressFieldType extends BaseFieldType implements IPreviewableFieldType
{
protected $countryCodes = array('AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW');

public function getName() {
return Craft::t('Address');
}
Expand All @@ -26,7 +24,7 @@ protected function defineSettings() {
}

public function getSettingsHtml() {
$countries = $this->_getCountryNames();
$countries = craft()->vzAddress->getCountries();

return craft()->templates->render('vzaddress/fieldtype/settings', array(
'settings' => $this->getSettings(),
Expand All @@ -53,7 +51,7 @@ public function getInputHtml($name, $value) {
craft()->templates->includeJsResource('vzaddress/js/input.js');
craft()->templates->includeJs("$('#{$namespacedId}').vzAddress();");

$countries = $this->_getCountryNames();
$countries = craft()->vzAddress->getCountries();

return craft()->templates->render('vzaddress/fieldtype/input', array(
'id' => $inputId,
Expand All @@ -77,15 +75,4 @@ public function getTableAttributeHtml($value) {
return $value->html();
}
}

private function _getCountryNames() {
$localeData = craft()->i18n->getLocaleData();
$countries = array();

foreach ($this->countryCodes as $code) {
$countries[$code] = $localeData->getTerritory($code);
}

return $countries;
}
}
18 changes: 18 additions & 0 deletions vzaddress/services/VzAddressService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Craft;

class VzAddressService extends BaseApplicationComponent
{
protected $countryCodes = array('AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW');

public function getCountries() {
$localeData = craft()->i18n->getLocaleData();
$countries = array();

foreach ($this->countryCodes as $code) {
$countries[$code] = $localeData->getTerritory($code);
}

return $countries;
}
}
15 changes: 15 additions & 0 deletions vzaddress/variables/VzAddressVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Craft;

class VzAddressVariable
{
/**
* Return an array of country ids and names
*
* @return array Countries indexed by ID
*/
public function countries()
{
return craft()->vzAddress->getCountries();
}
}

0 comments on commit 4bab6eb

Please sign in to comment.