You are here

class CountryManager in Ubercart 8.4

Provides list of countries.

Hierarchy

Expanded class hierarchy of CountryManager

File

uc_country/src/CountryManager.php, line 14

Namespace

Drupal\uc_country
View source
class CountryManager implements CountryManagerInterface {
  use StringTranslationTrait;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Stores the entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * An array of country code => country name pairs.
   *
   * @var array
   */
  protected $countries;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getList() {

    // Populate the country list if it is not already populated.
    if (!isset($this->countries)) {
      $this->countries = CoreCountryManager::getStandardList();
      $this->moduleHandler
        ->alter('countries', $this->countries);
    }
    return $this->countries;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableList() {
    $countries = $this->entityTypeManager
      ->getStorage('uc_country')
      ->loadMultiple(NULL);
    $country_names = [];
    foreach ($countries as $alpha_2 => $country) {

      // We can use non-literals in t() here because the country names are
      // defined in configuration files, so they have been translated.
      $country_names[$alpha_2] = $this
        ->t($country
        ->getName());
    }
    natcasesort($country_names);
    $this->moduleHandler
      ->alter('countries', $country_names);
    return $country_names;
  }

  /**
   * {@inheritdoc}
   */
  public function getEnabledList() {
    $countries = $this->entityTypeManager
      ->getStorage('uc_country')
      ->loadByProperties([
      'status' => TRUE,
    ]);
    $country_names = [];
    foreach ($countries as $alpha_2 => $country) {

      // We can use non-literals in t() here because the country names are
      // defined in configuration files, so they have been translated.
      $country_names[$alpha_2] = $this
        ->t($country
        ->getName());
    }
    natcasesort($country_names);
    $this->moduleHandler
      ->alter('countries', $country_names);
    return $country_names;
  }

  /**
   * {@inheritdoc}
   */
  public function getCountry($alpha_2) {
    return $this->entityTypeManager
      ->getStorage('uc_country')
      ->load($alpha_2);
  }

  /**
   * {@inheritdoc}
   */
  public function getByProperty(array $properties) {
    $countries = $this->entityTypeManager
      ->getStorage('uc_country')
      ->loadByProperties($properties);
    $country_names = [];
    foreach ($countries as $alpha_2 => $country) {

      // We can use non-literals in t() here because the country names are
      // defined in configuration files, so they have been translated.
      $country_names[$alpha_2] = $this
        ->t($country
        ->getName());
    }
    natcasesort($country_names);
    return $country_names;
  }

  /**
   * {@inheritdoc}
   */
  public function getZoneList($alpha_2) {
    if ($country = $this->entityTypeManager
      ->getStorage('uc_country')
      ->load($alpha_2)) {
      return $country
        ->getZones();
    }
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountryManager::$countries protected property An array of country code => country name pairs.
CountryManager::$entityTypeManager protected property Stores the entity type manager.
CountryManager::$moduleHandler protected property The module handler service.
CountryManager::getAvailableList public function Returns a list of all available country code => country name pairs. Overrides CountryManagerInterface::getAvailableList
CountryManager::getByProperty public function Returns all uc_country config entities with the specified propertes. Overrides CountryManagerInterface::getByProperty
CountryManager::getCountry public function Returns the uc_country config entity with the specified country code. Overrides CountryManagerInterface::getCountry
CountryManager::getEnabledList public function Returns a list of country code => country name pairs for enabled countries. Overrides CountryManagerInterface::getEnabledList
CountryManager::getList public function Returns a list of country code => country name pairs. Overrides CountryManagerInterface::getList
CountryManager::getZoneList public function Returns a list of zone code => zone name pairs for the specified country. Overrides CountryManagerInterface::getZoneList
CountryManager::__construct public function Constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.