CountryManager.php in Ubercart 8.4
File
uc_country/src/CountryManager.php
View source
<?php
namespace Drupal\uc_country;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Locale\CountryManager as CoreCountryManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class CountryManager implements CountryManagerInterface {
use StringTranslationTrait;
protected $moduleHandler;
protected $entityTypeManager;
protected $countries;
public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslation = $string_translation;
}
public function getList() {
if (!isset($this->countries)) {
$this->countries = CoreCountryManager::getStandardList();
$this->moduleHandler
->alter('countries', $this->countries);
}
return $this->countries;
}
public function getAvailableList() {
$countries = $this->entityTypeManager
->getStorage('uc_country')
->loadMultiple(NULL);
$country_names = [];
foreach ($countries as $alpha_2 => $country) {
$country_names[$alpha_2] = $this
->t($country
->getName());
}
natcasesort($country_names);
$this->moduleHandler
->alter('countries', $country_names);
return $country_names;
}
public function getEnabledList() {
$countries = $this->entityTypeManager
->getStorage('uc_country')
->loadByProperties([
'status' => TRUE,
]);
$country_names = [];
foreach ($countries as $alpha_2 => $country) {
$country_names[$alpha_2] = $this
->t($country
->getName());
}
natcasesort($country_names);
$this->moduleHandler
->alter('countries', $country_names);
return $country_names;
}
public function getCountry($alpha_2) {
return $this->entityTypeManager
->getStorage('uc_country')
->load($alpha_2);
}
public function getByProperty(array $properties) {
$countries = $this->entityTypeManager
->getStorage('uc_country')
->loadByProperties($properties);
$country_names = [];
foreach ($countries as $alpha_2 => $country) {
$country_names[$alpha_2] = $this
->t($country
->getName());
}
natcasesort($country_names);
return $country_names;
}
public function getZoneList($alpha_2) {
if ($country = $this->entityTypeManager
->getStorage('uc_country')
->load($alpha_2)) {
return $country
->getZones();
}
return [];
}
}