You are here

public function GreatBritainEventSubscriber::onSubdivisions in Address 8

Provides the subdivisions for Great Britain.

Note: Provides just the Welsh counties. A real subscriber would include the full list, sourced from the CLDR "Territory Subdivisions" listing.

Parameters

\Drupal\address\Event\SubdivisionsEvent $event: The subdivisions event.

File

tests/modules/address_test/src/EventSubscriber/GreatBritainEventSubscriber.php, line 53

Class

GreatBritainEventSubscriber
Adds a county field and a predefined list of counties for Great Britain.

Namespace

Drupal\address_test\EventSubscriber

Code

public function onSubdivisions(SubdivisionsEvent $event) {

  // For administrative areas $parents is an array with just the country code.
  // Otherwise it also contains the parent subdivision codes. For example,
  // if we were defining cities in California, $parents would be ['US', 'CA'].
  $parents = $event
    ->getParents();
  if ($event
    ->getParents() != [
    'GB',
  ]) {
    return;
  }
  $definitions = [
    'country_code' => $parents[0],
    'parents' => $parents,
    'subdivisions' => [
      // Key by the subdivision code, which is the value that's displayed on
      // the formatted address. Could be an abbreviation (e.g 'CA' for
      // California) or a full name like below.
      // If it's an abbreviation, define a 'name' in the subarray, to be used
      // in the address widget dropdown.
      'Anglesey' => [],
      // You can optionally define an ISO 3166-2 code for each subdivision.
      'Blaenau Gwent' => [
        'iso_code' => 'GB-BGW',
      ],
      'Bridgend' => [],
      'Caerphilly' => [],
      'Cardiff' => [],
      'Carmarthenshire' => [],
      'Ceredigion' => [],
      'Conwy' => [],
      'Denbighshire' => [],
      'Flintshire' => [],
      'Gwynedd' => [],
      'Merthyr Tydfil' => [],
      'Monmouthshire' => [],
      'Neath Port Talbot' => [],
      'Newport' => [],
      'Pembrokeshire' => [],
      'Powys' => [],
      'Rhondda Cynon Taf' => [],
      'Swansea' => [],
      'Tarfaen' => [],
      'Vale of Glamorgan' => [],
      'Wrexham' => [],
    ],
  ];
  $event
    ->setDefinitions($definitions);
}