In drupal 8 we can get country list using following code.

$countries = \Drupal::service('country_manager')->getList();

But how can I programatically get the list of state of each country?


Drupal core only keeps a record of countries, not all the states/administrative areas within each country.

However the CommerceGuys addressing library has that functionality. Example from their readme:

use CommerceGuys\Addressing\Subdivision\SubdivisionRepository;

$subdivisionRepository = new SubdivisionRepository();

// Get the subdivisions for Brazil.
$states = $subdivisionRepository->getAll(['BR']);
foreach ($states as $state) {
    $municipalities = $state->getChildren();

If need to get list of states as indexed array use the function 'getList()':

$states = $subdivisionRepository->getList(['BR']);

Then if you print the $states, you will get:

    [AC] => Acre
    [AL] => Alagoas
    [AP] => Amapá
    [AM] => Amazonas
    [BA] => Bahia
    [CE] => Ceará
    [DF] => Distrito Federal
    [ES] => Espírito Santo
    [GO] => Goiás
    [MA] => Maranhão
    [MT] => Mato Grosso
    [MS] => Mato Grosso do Sul
    [MG] => Minas Gerais
    [PA] => Pará
    [PB] => Paraíba
    [PR] => Paraná
    [PE] => Pernambuco
    [PI] => Piauí
    [RJ] => Rio de Janeiro
    [RN] => Rio Grande do Norte
    [RS] => Rio Grande do Sul
    [RO] => Rondônia
    [RR] => Roraima
    [SC] => Santa Catarina
    [SP] => São Paulo
    [SE] => Sergipe
    [TO] => Tocantins
  • Is there any code snippet available to check how to get state list using Address module?
    – ARUN
    Jun 6 '17 at 7:51
  • @Clive, Thanks Clive. To add this code Address module should be enabled in the site. right?
    – ARUN
    Jun 6 '17 at 9:11
  • @Clive, I didn't enable the Address module and added the code you mentioned above and while printing I am getting following: codeshare.io/a3DQ3k. I'm not able to access any private property of that object.
    – ARUN
    Jun 6 '17 at 9:19
  • That's the example on their documentation page, if it doesn't work I'd raise a bug report
    – Clive
    Jun 6 '17 at 9:31
  • Its providing that data object. But I could't find how to get data from object.
    – ARUN
    Jun 6 '17 at 9:51

The state list is not in drupal core. You need to get a feed from somewhere else.

Consider using the address module. This uses the commerce addressing library, providing you the state lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.