Country API
New in version 12.2
TYPO3 ships a list of countries of the world. The list is based on the ISO 3166-1 standard, with the alphanumeric short name ("FR" or "FRA" in its three-letter short name), the English name ("France"), the official name ("Republic of France"), also the numerical code, and the country's flag as emoji (UTF-8 representation).
Note
The country list is based on Debian's ISO code list and shipped statically as PHP content in the Country API.
Contents
Using the PHP API
Dependency injection can be used to retrieve the
\TYPO3\
class:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
use TYPO3\CMS\Core\Country\CountryProvider;
final class MyClass
{
public function __construct(
private readonly CountryProvider $countryProvider,
) {}
}
Get all countries
To get all countries call the get
method:
$allCountries = $this->countryProvider->getAll();
The method returns an array of \TYPO3\
objects.
Get a country
// Get the country by Alpha-2 code
$france = $this->countryProvider->getByIsoCode('FR');
// Get the country by name
$france = $this->countryProvider->getByEnglishName('France');
// Get the country by Alpha-3 code
$france = $this->countryProvider->getByAlpha3IsoCode('FRA');
The methods return a \TYPO3\
object.
Filter countries
One can use filters to get the desired countries:
use TYPO3\CMS\Core\Country\CountryFilter;
$filter = new CountryFilter();
// Alpha-2 and Alpha-3 ISO codes can be used
$filter
->setOnlyCountries(['AT', 'DE', 'FR', 'DK'])
->setExcludeCountries(['AUT', 'DK']);
// Will be an array with "Germany" and "France"
$filteredCountries = $this->countryProvider->getFiltered($filter);
The method get
return an array of
\TYPO3\
objects.
The Country
object
A country object can be used to fetch all information about it, also with translatable labels:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension;
use TYPO3\CMS\Core\Country\CountryProvider;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Localization\Locale;
final class MyClassWithTranslation
{
public function __construct(
private readonly CountryProvider $countryProvider,
private readonly LanguageServiceFactory $languageServiceFactory,
) {}
public function doSomething()
{
$languageService = $this->languageServiceFactory->create(new Locale('de'));
$france = $this->countryProvider->getByIsoCode('FR');
// "France"
$france->getName();
// "Frankreich"
$languageService->sL($france->getLocalizedNameLabel());
// "French Republic"
echo $france->getOfficialName();
// "Französische Republik"
$languageService->sL($france->getLocalizedOfficialNameLabel());
// 250
$france->getNumericRepresentation();
// "FR"
$france->getAlpha2IsoCode();
// "🇫🇷"
$france->getFlag();
}
}
PHP API reference
CountryProvider
- class CountryProvider
-
- Fully qualified name
-
\TYPO3\
CMS\ Core\ Country\ Country Provider
A class providing information about all countries.
Country data is generated from "Build/Scripts/updateIsoDatabase.php" (which in turn stems from https://github.com/sokil/php-isocodes-db-i18n)
- getByAlpha2IsoCode ( string $isoCode)
-
- param $isoCode
-
the isoCode
- Returns
-
?\
TYPO3\ CMS\ Core\ Country\ Country
CountryFilter
Country
Form ViewHelper
A Fluid ViewHelper is shipped with TYPO3 to render a dropdown for forms. See f:form.countrySelect for more information.