2016-08-30 22 views
3

在SilverStripe由CountryDropdownField产生一个下拉菜单的默认顺序是按字母顺序排列:如何在SilverStripe中订购CountryDropDownfield中的国家?

  • 阿富汗
  • 奥兰群岛
  • 阿尔巴尼亚
  • 阿尔及利亚

哪有下拉菜单被排序,所以普通国家位列榜首由较少使用的国家的字母表列表结婚?

  • 澳大利亚
  • 加拿大
  • 法国
  • 德国
  • 新西兰
  • 南非
  • 英国
  • 美国
  • 阿富汗
  • 奥兰群岛
  • 阿尔巴尼亚
  • 阿尔及利亚

回答

4

我们可以创造我们自己国家的数组,并使用CountryDropdownFieldsetSource功能设定国家的顺序:

$countriesList = Zend_Locale::getTranslationList('territory', i18n::get_locale(), 2); 
asort($countriesList, SORT_LOCALE_STRING); 

$commonCountries = array(
    'AU' => 'Australia', 
    'CA' => 'Canada', 
    'FR' => 'France', 
    'DE' => 'Germany', 
    'NZ' => 'New Zealand', 
    'ZA' => 'South Africa', 
    'GB' => 'United Kingdom', 
    'US' => 'United States' 
); 

CountryDropdownField::create('Country', 'Country') 
    ->setSource(array_merge($commonCountries, $countriesList)); 

或使用uksort代替合并阵列:

$countriesList = Zend_Locale::getTranslationList('territory', i18n::get_locale(), 2); 
$commonCountries = array('AU', 'CA', 'FR', 'DE', 'NZ', 'ZA', 'GB', 'US'); 

uksort($countriesList, function ($a, $b) use ($countriesList, $commonCountries) { 
    if (in_array($a, $commonCountries)) { 
     if (in_array($b, $firstCountries)) { 
      return ($countriesList[$a] < $countriesList[$b]) ? -1 : 1; 
     } 
     return -1; 
    } 
    if (in_array($b, $commonCountries)) { 
     return 1; 
    } 
    return ($countriesList[$a] < $countriesList[$b]) ? -1 : 1; 
}); 

CountryDropdownField::create('Country', 'Country')->setSource($countriesList);