选择下拉列表中的Prestashop php formfield设置值

选择下拉列表中的Prestashop php formfield设置值,php,forms,drop-down-menu,prestashop,formfield,Php,Forms,Drop Down Menu,Prestashop,Formfield,我有一个用FormField从php创建的表单,还有一个country字段,其中包含一个国家列表。我的问题是如何从php而不是html模板中设置要选择的国家(因为它在tpl文件中被创建为{form_field field=$field}) 这是我的密码: $countries = Country::getCountries($this->context->language->id); $format['country'] = (new FormField)

我有一个用FormField从php创建的表单,还有一个country字段,其中包含一个国家列表。我的问题是如何从php而不是html模板中设置要选择的国家(因为它在tpl文件中被创建为{form_field field=$field})

这是我的密码:

$countries = Country::getCountries($this->context->language->id);
    $format['country'] = (new FormField)
        ->setName('country')
        ->setType('countrySelect')
        ->setLabel(
            $this->translator->trans(
                'Country', [], 'Shop.Forms.Labels'
            )
        )
        ->setRequired(true)
    ;

    foreach ($countries as $country) {
        $format['country']->addAvailableValue(
            $country['id_country'],
            $country['country']
        );
    }
如果我可以从php设置它,那将是很棒的,因为我不想更改核心文件或其他东西。提前感谢。

(新表单字段)->setValue($value)
应该完成这项工作,请查看
classes/form/FormField.php

public function setValue($value)

现在,如果计划设置相对必需值,则需要在类内创建一个私有变量:

              $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']);
                }
              $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']);
                }