Override country list in 5.7

Permalink
Is there an easy way to override the system country list to exclude extra countries and survive a core upgrade?

Located at src/Localization/Service/CountryList.php

regards
alex

 
JohntheFish replied on at Permalink Reply
JohntheFish
For 5.6 I built a Country/State manager addon to allow the list to be managed from the dashboard.https://www.concrete5.org/marketplace/addons/country-state-manager/... I find it pretty cool, but it didn't attract much of a marketplace following, so isn't high on my long list of addons to port to 5.7.
daenu replied on at Permalink Reply
daenu
Copy the
/concrete/src/Localization/Service/CountryList.php
into
/application/src/Localization/Service/CountryList.php


Adapt the namespace from
namespace Concrete\Core\Localization\Service;

to
namespace Application\Core\Localization\Service;


You're now free to add/remove methods to that class without having any risks when updating the site.
alexwag replied on at Permalink Reply
Thanks Daenu, I tried that but it seems I may be looking in the wrong place.

For example, I have tried remove two countries by unsetting them in loadCountries() in both the core and application version.

To remove Andorra & Antarctica, I've done the following..

$countries = \Punic\Territory::getCountries();
        unset(
            // Fake countries
            $countries['IM'], // Isle of Man (it's a British Crown Dependency)
            $countries['JE'], // Jersey (it's a British Crown Dependency)
            $countries['AD'], // Andorra
            $countries['AQ']  // Antarctica
        );


.. yet they are still present in any country list in the system.
daenu replied on at Permalink Reply
daenu
After digging a bit I found that the system parses the /concrete/languages & /application/languages folders to create that array. So or you delete the corresponding folder (?) or you override the Localization Class (/concrete/src/Localization/Localization.php). There is a method called "getAvailableInterfaceLanguages". So I defined an exclude array to check with in_array:
// ...
$excluded = array(
    'de_DE',
    'it_IT'
);
if (file_exists(DIR_LANGUAGES)) {
    $contents = $fh->getDirectoryContents(DIR_LANGUAGES);
    foreach ($contents as $con) {
        if (is_dir(DIR_LANGUAGES . '/' . $con) && file_exists(DIR_LANGUAGES . '/' . $con . '/LC_MESSAGES/messages.mo')) {
            if(!in_array($con, $excluded)){
                $languages[] = $con;
            }
        }
    }
}
alexwag replied on at Permalink Reply
hmm, that seems more related to languages.

I have discovered that making changes to..

/updates/concrete5.7.5.3_remote_updater/concrete/src/Localization/Service/CountryList.php

.. by unsetting array values in loadCountries() seems to works.

But obviously this won't survive a c5 upgrade.

It seems this cannot be overridden by copying into /application/src.
katalysis replied on at Permalink Reply
katalysis
Did anyone ever find a way to override src/Localization/Service/CountryList.php safely?

I need to add Jersey, Guernsey and Isle of Man (all British Crown dependencies) to the list for a Community Store site.
Qodo replied on at Permalink Best Answer Reply
This can be done by overriding the event:
on_get_countries_list


Events List:
https://documentation.concrete5.org/developers/appendix/full-event-l...

Hooking into events:
https://documentation.concrete5.org/developers/application-events/ho...

The easiest thing to do is add override code to
application/bootstrap/app.php
. You can either replace list of countries with something like this:

Events::addListener('on_get_countries_list', function($event) {
    // Create our list of countries
    $event['countries'] = array(
        'GB' => 'United Kingdom',
    );
    return $event;
});


Or remove the countries you don't want like this:

Events::addListener('on_get_countries_list', function($event) {
    // Remove United Kingdom
    unset($event['countries']['GB'];
    return $event;
});