模块中的 Prestashop HelpForm 类型开关

Prestashop HelpForm type switch in module

我想在 prestashop 1.6 的后端模块中创建一个输入类型开关。 我写这个并工作

array(
    'type' => 'switch',
    'label' => $this->l('Label'),
    'name' => 'PRESTASHOP_INPUT_SWITCH',
    'is_bool' => true,
    'desc' => $this->l('Description'),
    'values' => array(
        array(
            'id' => 'active_on',
            'value' => true,
            'label' => $this->l('Enabled')
        ),
        array(
            'id' => 'active_off',
            'value' => false,
            'label' => $this->l('Disabled')
        )
    ),
)

但是如果我尝试使用自定义值 e 不是布尔值它不起作用

array(
    'type' => 'switch',
    'label' => $this->l('Label'),
    'name' => 'PRESTASHOP_INPUT_SWITCH',
    'is_bool' => false,
    'desc' => $this->l('Description'),
    'values' => array(
        array(
            'id' => 'value1',
            'value' => 'value1',
            'label' => $this->l('value1')
        ),
        array(
            'id' => 'value2',
            'value' => 'value2',
            'label' => $this->l('value2')
        )
    ),
)

在后端出现两个带有标签值 'no' 的框。 在 HelperForm 类 的 prestashop 中没有输入类型切换的痕迹。 与类型收音机工作相同的代码,但我想要一个开关类型。

不幸的是,正如您在此处看到的那样: PrestaShop 无法在 "switch" 字段中使用自定义值。

您不需要更改默认开关值。

这是您处理提交表单的方式:

public function getContent()
{
    if (Tools::isSubmit('submitForm'))
    {
  $my_value = ( (int)Tools::getValue('PRESTASHOP_INPUT_SWITCH') == 1 ) ? 'value1' : 'value2';
  if (Configuration::updateValue('PRESTASHOP_INPUT_SWITCH', $my_value))
            $echo .= $this->displayConfirmation($this->l('Value updated.'));

    }
    $echo .= $this->renderForm();

    return $echo;
}

你可能已经有了这样的东西:

public function renderForm()
{

    $fields_form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Settings'),
                'icon' => 'icon-cogs'
            ),
            'input' => array(

        array(
            'type' => 'switch',
            'label' => $this->l('Label'),
            'name' => 'PRESTASHOP_INPUT_SWITCH',
            'is_bool' => true,
            'desc' => $this->l('Description'),
            'values' => array(
                array(
                    'id' => 'active_on',
                    'value' => 1,
                    'label' => $this->l('Value1')
                ),
                array(
                    'id' => 'active_off',
                    'value' => 0,
                    'label' => $this->l('Value2')
                )
            ),
        )
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            )
        ),
    );

    $helper = new HelperForm();
    $helper->module = $this;
    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $this->fields_form = array();

    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitForm';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->tpl_vars = array(
        'fields_value' => $this->getConfigFieldsValues(),
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id
    );

    return $helper->generateForm(array($fields_form));
}

并加载值:

public function getConfigFieldsValues()
{
    return array(
        'PRESTASHOP_INPUT_SWITCH' => Tools::getValue('PRESTASHOP_INPUT_SWITCH', Configuration::get('PRESTASHOP_INPUT_SWITCH') == 'value1' : 1 : 0),
    );
}

很遗憾,prestashop 不支持 switch 的自定义值,但您可以这样处理:

$config = Configuration::get('oneclickcheckout');
     $this->settings = Tools::unSerialize($config);
     $settings = $this->settings;
     if (isset($settings['Description']) && $settings['Description'] == 1) {

    }