Skip to content

Commit

Permalink
Issue #3111375 by SKAUGHT, OPTASY: Webform should only list approved …
Browse files Browse the repository at this point in the history
…GoC recognized countries
  • Loading branch information
skaught authored and sylus committed Nov 2, 2023
1 parent 2784e26 commit f6489d8
Show file tree
Hide file tree
Showing 9 changed files with 2,083 additions and 0 deletions.
1,506 changes: 1,506 additions & 0 deletions modules/custom/wxt_core/config/data/sccai_county_names.yml

Large diffs are not rendered by default.

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

namespace Drupal\wxt_core;

use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Locale\CountryManagerInterface;

/**
*
*/
class CountriesManager {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The module extension list.
*
* @var \Drupal\Core\Extension\ModuleExtensionList
*/
protected $moduleExtensionList;

/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* UpdateCommand constructor.
*
* @param \Traversable $namespaces
* The namespaces to scan for updates.
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
* The class resolver service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
* The module extension list.
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
* (optional) The update discovery handler.
* @param \phpDocumentor\Reflection\DocBlockFactoryInterface $doc_block_factory
* (optional) The doc block factory.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleExtensionList $module_extension_list, CountryManagerInterface $country_manager, ModuleHandlerInterface $module_handler) {
$this->configFactory = $config_factory;
$this->moduleExtensionList = $module_extension_list;
$this->countryManager = $country_manager;
$this->moduleHandler = $module_handler;
}

/**
* Get all data (from yml) expect for this issue[#3111375] from original online PDF.
*/
public function getCountryData($column = 'Alpha-2', $row = 'Code') {
$module_path = $this->moduleHandler->getModule('wxt_core')->getPath();
$country_data = Yaml::decode(\file_get_contents(DRUPAL_ROOT . '/' . $module_path . '/config/data/sccai_county_names.yml'));
if ($column === NULL || $column === FALSE) {
return $country_data;
}
$country_names = [];

foreach ($country_data as $key => $data) {
$country_names[$data[$column]] = $data[$row];
}
return $country_names;
}

/**
* Get full, normal list of Countries.
*/
public function listCounties() {
$list = $this->countryManager->getList();
$countries = [];
foreach ($list as $key => $value) {
$val = $value->__toString();
$countries[$key] = $val;
}
return $countries;
}

/**
* Returns the whitelisted approved items.
*/
public function whitelistedOptions() {
$standartCountries = $this->listCounties();
$config = $this->configFactory->get('wxt_core_countries.settings');
$countries = $config->get('countries');
foreach ($countries as $abbr => $checkbox) {
if ($checkbox != 0) {
$options[$standartCountries[$abbr]] = $standartCountries[$abbr];
}
}
return $options;
}

/**
* Returns the sorted whitelisted.
*/
public function whitelistedOptionsSorted() {
$config = $this->configFactory->get('wxt_core_countries.settings');
$whitelist = $this->whitelistedOptions();
switch ($config->get('whitelist-sort')){
case 'alpha':
\ksort($whitelist);
break;
}
return $whitelist;
}

}
Loading

0 comments on commit f6489d8

Please sign in to comment.