This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathAddCountriesAsStatesSubscriber.php
More file actions
67 lines (58 loc) · 1.74 KB
/
AddCountriesAsStatesSubscriber.php
File metadata and controls
67 lines (58 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Drupal\va_gov_address\EventSubscriber;
use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\SubdivisionsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Adds select countries to the US States.
*
* This class follows the Centarro guidelines:
* https://docs.drupalcommerce.org/v2/developer-guide/customers/addresses/#how-do-i-add-or-modify-subdivisions-for-a-country.
*/
class AddCountriesAsStatesSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AddressEvents::SUBDIVISIONS][] = ['onSubdivisions'];
return $events;
}
/**
* Provides the states of the US (plus the Philippines).
*
* @param \Drupal\address\Event\SubdivisionsEvent $event
* The subdivisions event.
*/
public function onSubdivisions(SubdivisionsEvent $event) {
if ($event->getParents() !== ['US']) {
return;
}
$definitions = $event->getDefinitions();
// Add the Philippines as a state.
$definitions['subdivisions']['PH'] = [
'code' => 'PH',
'name' => 'Philippines',
'country_code' => 'US',
'id' => 'PH',
];
// Add Germany as a state.
$definitions['subdivisions']['DEU'] = [
'code' => 'DEU',
'name' => 'Germany',
'country_code' => 'US',
'id' => 'DEU',
];
// Add South Korea as a state.
$definitions['subdivisions']['KR'] = [
'code' => 'KR',
'name' => 'South Korea',
'country_code' => 'US',
'id' => 'KR',
];
// Sort subdivisions by name.
uasort($definitions['subdivisions'], function ($a, $b) {
return strcmp($a['name'], $b['name']);
});
$event->setDefinitions($definitions);
}
}