class XXXFormatter extends XXXCore
{
private $country = 666;// 666 = preselected country by id
...
}
或者
class XXXFormatter extends XXXCore
{
private $country = (int) Tools::getValue('id_country');
...
public function getFormat()
{
...
if($this->country == 666)
{
$format['country'] = (new FormField())
->setName('id_country')
->setType('hidden')
->setValue($this->country);
}
else
{
$countries = Country::getCountries($this->language->id,true);
$format['country'] = (new FormField())
->setName('country')
->setType('countrySelect')
->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
->setRequired($this->country_is_required)
->setValue($this->country);
foreach ($countries as $country) {
$format['country']->addAvailableValue(
$country['id_country'],
$country['name']);
}
}
...
}
}
现在,如果您打算设置一个相对所需的值,您需要在类中创建一个私有变量:
$countries = Country::getCountries($this->language->id,true);
$format['country'] = (new FormField())
->setName('country')
->setType('countrySelect')
->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
->setRequired($this->country_is_required)
->setValue($this->country);
foreach ($countries as $country) {
$format['country']->addAvailableValue(
$country['id_country'],
$country['name']);
}
或设置不同的私有变量:
$countries = Country::getCountries($this->language->id,true);
$format['country'] = (new FormField())
->setName('country')
->setType('countrySelect')
->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
->setRequired($this->password_is_required)
->setValue($this->country);
foreach ($countries as $country) {
$format['country']->addAvailableValue(
$country['id_country'],
$country['name']);
}