Version: current

     1 <?php
     2 /**
     3  * A simple extension to dropdown field, pre-configured to list countries.
     4  * It will default to the country of the current visiotr.
     5  * @package forms
     6  * @subpackage fields-relational
     7  */
     8 class CountryDropdownField extends DropdownField {
     9     protected $defaultToVisitorCountry = true;
    10
    11     function __construct($name, $title = null, $source = null, $value = "", $form=null) {
    12         if(!is_array($source)) $source = Geoip::getCountryDropDown();
    13         if(!$value) $value = Geoip::visitor_country();
    14
    15         parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form);
    16     }
    17
    18     function defaultToVisitorCountry($val) {
    19         $this->defaultToVisitorCountry = $val;
    20     }
    21
    22     function Field() {
    23         $source = $this->getSource();
    24         if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
    25             $this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::$default_country_code;
    26         }
    27         return parent::Field();
    28     }
    29 }
    30
    31 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -