gertvdb/iso3166

基于ISO3611数据提供国家和大陆插件

维护者

详细信息

github.com/gertvdb/iso3166

来源

问题

安装: 36

依赖: 2

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 4

类型:drupal-module

8.1.1 2020-10-29 11:06 UTC

README

Build Status

描述

本模块基于官方iso3166数据提供国家和大陆。它提供国家和大陆作为插件,并将它们映射在一起。由于国家和大陆是插件,最终用户可以轻松添加自己的或覆盖现有的。

安装

要安装此模块,请按照以下步骤操作

使用composer

  1. composer require gertvdb/iso3166

示例

下面可以找到一个如何使用iso3166模块的示例。该模块提供了一个服务来处理插件数据。

使用插件管理器。

插件管理器提供了一些额外的功能,以方便地获取国家和大陆数据。

  /** @var \Drupal\iso3166\Plugin\CountryManager $pluginManager */
  $pluginManager = \Drupal::service('plugin.manager.country');
  
  $belguim = $pluginManager->getCountry('BE');
  $allCountries = $pluginManager->getCountries();
  
  /** @var \Drupal\iso3166\Plugin\ContinentManager $pluginManager */
  $pluginManager = \Drupal::service('plugin.manager.continent');
  
  $europe = $pluginManager->getContinent('EU');
  $allContinents = $pluginManager->getContinents();
  

使用服务。

服务提供了多种方法来处理国家和大陆数据。

  /** @var \Drupal\iso3166\Iso3166 $service */
  $service = \Drupal::service('iso3166');
  
  $continentOfBelgium = $service->getContinentByCountry('BE');
  $allCountriesInEurope = $service->getCountriesByContinent('EU');
  

更改插件数据。

要更改插件中的数据,您需要提供一个alter钩子。或者,如果您不喜欢钩子,可以查看 hook_event_dispatcher 模块并触发iso3166_country_info_alter事件。

  /**
   * Implements hook_iso3166_country_info_alter().
   */
  function MY_MODULE_iso3166_country_info_alter(array &$definitions) {
    $definitions['country:BE']['label'] = t('Other name for belgium');
  }
  

添加数据。

由于大陆、国家和县集合都是插件,因此您可以轻松提供自己的。以下示例将“Neverland”添加到欧洲国家的列表中。但您也可以以相同的方式提供一个新的大陆来添加。

   
  namespace Drupal\MY_MODULE\Plugin\iso3166\Country;
  
  use Drupal\Core\Annotation\Translation;
  use Drupal\iso3166\Annotation\Country;
  use Drupal\iso3166\Plugin\iso3166\Country\CountryPluginBase;
  
  /**
   * Provides a country.
   *
   * @Country(
   *   id = "country_neverland",
   *   label = @Translation("Neverland"),
   *   alpha2 = "NV",
   *   alpha3 = "NVL",
   *   numeric = "999",
   *   continent = "EU"
   * )
   */
  class Neverland extends CountryPluginBase {}

  
   
  namespace Drupal\MY_MODULE\Plugin\iso3166\Continent;
  
  use Drupal\Core\Annotation\Translation;
  use Drupal\iso3166\Annotation\Continent;
  use Drupal\iso3166\Plugin\Iso3166\Continent\ContinentPluginBase;
  
  /**
   * Provides a continent.
   *
   * @Continent(
   *   id = "continent_zealandia",
   *   label = @Translation("Zealandia"),
   *   alpha2 = "ZL",
   * )
   */
  class Zealandia extends ContinentPluginBase {}

  
   
    namespace Drupal\MY_MODULE\Plugin\iso3166\CountryCollection;
    
    use Drupal\Core\Annotation\Translation;
    use Drupal\iso3166\Annotation\CountryCollection;
    use Drupal\iso3166\Plugin\Iso3166\CountryCollection\CountryCollectionPluginBase;
    
    /**
     * Provides a country collection.
     *
     * @CountryCollection(
     *   id = "benelux",
     *   label = @Translation("Benelux"),
     *   countries={"BE","NL","LU"}
     * )
     */
    class Benelux extends CountryCollectionPluginBase {}