BeforeCountriesEvaluatedEvent 

The PSR-14 event \TYPO3\CMS\Core\Country\Event\BeforeCountriesEvaluatedEvent allows to modify the list of countries provided by the \TYPO3\CMS\Core\Country\CountryProvider .

This event allows to to add, remove and alter countries from the list used by the provider class itself and ViewHelpers like the Form.countrySelect ViewHelper <f:form.countrySelect>.

Example: add a new country to the country selectors 

The following event listener adds a new country, 'Magic Kingdom' with alpha 2 code 'XX' and alpha 3 code 'XXX'.

EXT:my_extension/Classes/Country/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Country\Country;
use TYPO3\CMS\Core\Country\Event\BeforeCountriesEvaluatedEvent;

final readonly class EventListener
{
  #[AsEventListener(identifier: 'my-extension/before-countries-evaluated')]
  public function __invoke(BeforeCountriesEvaluatedEvent $event): void
  {
    $countries = $event->getCountries();
    unset($countries['BS']);
    $countries['XX'] = new Country(
      'XX',
      'XYZ',
      'Magic Kingdom',
      '987',
      '🔮',
      'Kingdom of Magic and Wonders',
    );
    $event->setCountries($countries);
  }
}
Copied!

Changed in version 14.0

As the localized names for the countries are defined in file EXT:core/Resources/Private/Language/Iso/countries.xlf, this language file needs to be extended via $GLOBALS['TYPO3_CONF_VARS']['LANG']['resourceOverrides']:

EXT:my_extension/ext_localconf.php
<?php

$GLOBALS['TYPO3_CONF_VARS']['LANG']['resourceOverrides']
['EXT:core/Resources/Private/Language/Iso/countries.xlf'][]
    = 'EXT:my_extension/Resources/Private/Language/countries.xlf';
Copied!

You can now override the language file in the path defined above:

EXT:my_extension/Resources/Private/Language/countries.xlf
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
  <file source-language="en" datatype="plaintext" date="2024-01-08T18:44:59Z" product-name="my_extension">
    <body>
      <trans-unit id="XX.name" approved="yes">
        <source>Magic Kingdom</source>
      </trans-unit>
      <trans-unit id="XX.official_name" approved="yes">
        <source>Kingdom of Magic and Wonders</source>
      </trans-unit>
    </body>
  </file>
</xliff>
Copied!

And add additional translations for German:

EXT:my_extension/Resources/Private/Language/de.countries.xlf
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
  <file source-language="en" target-language="de" datatype="plaintext" date="2024-01-08T18:44:59Z" product-name="my_extension">
    <body>
      <trans-unit id="XX.name" approved="yes">
        <source>Magic Kingdom</source>
        <target>Magisches Königreich</target>
      </trans-unit>
      <trans-unit id="XX.official_name" approved="yes">
        <source>Kingdom of Magic and Wonders</source>
        <target>Königreich der Magie und Wunder</target>
      </trans-unit>
    </body>
  </file>
</xliff>
Copied!

And Klingon:

EXT:my_extension/Resources/Private/Language/tlh.countries.xlf
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
  <file source-language="en" target-language="tlh" datatype="plaintext" date="2024-01-08T18:44:59Z" product-name="my_extension">
    <body>
      <trans-unit id="XX.name" approved="yes">
        <source>Magic Kingdom</source>
        <target>‘oHtaHghach wo’</target>
      </trans-unit>
      <trans-unit id="XX.official_name" approved="yes">
        <source>Kingdom of Magic and Wonders</source>
        <target>‘oHtaHghach je Dojmey wo’</target>
      </trans-unit>
    </body>
  </file>
</xliff>
Copied!

API of event BeforeCountriesEvaluatedEvent 

final class BeforeCountriesEvaluatedEvent
Fully qualified name
\TYPO3\CMS\Core\Country\Event\BeforeCountriesEvaluatedEvent

Event dispatched before countries are evaluated by CountryProvider.

getCountries()
Returns
\TYPO3\CMS\Core\Country\Country[]
setCountries(\TYPO3\CMS\Core\Country\Country[] $countries)
param $countries

the countries