Prestashop 预设帮助执行:切换按钮

Prestashop 预设帮助执行:切换按钮,prestashop,Prestashop,我正在使用PrestashopHelperForm生成一个开关按钮。按钮的状态取决于从数据库生成的数据。问题是按钮总是设置为false 代码如下: $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->l('Champs pour feuille de soin'), 'i

我正在使用Prestashop
HelperForm
生成一个开关按钮。按钮的状态取决于从数据库生成的数据。问题是按钮总是设置为false

代码如下:

$fields_form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Champs pour feuille de soin'),
                'icon' => 'icon-pencil'
            ),

            'input' => array(
                array(
                    'type' => 'hidden',
                    'name' => 'id_product',
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu1'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field1',
                ),

                array(
                    'type' => 'radio',
                    'label' => $this->l('required'),
                    'name' => 'relab1',
                    'is_bool' => false,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'label1_on',
                            'value' => 1,
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'label1_off',
                            'value' => 0,
                            'label' => $this->l('Disabled')
                        )
                    )
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu2'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field2',
                ),
                array(
                    'type' => 'radio',
                    'label' => $this->l('required'),
                    'name' => 'relab2',
                    'is_bool' => false,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'active_on',
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'active_off',
                            'label' => $this->l('Disabled')
                        )
                    )
                ),


                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu3'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field3',
                ),

                array(
                    'type' => 'switch',
                    'label' => $this->l('required'),
                    'name' => 'relab3',
                    'is_bool' => true,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'label3_on',
                            'value' => 1,
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'label3_off',
                            'value' => 0,
                            'label' => $this->l('Disabled')
                        )
                    )
                ),


                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu4'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field4',
                ),

                array(
                    'type' => 'checkbox',
                    'label' => $this->l('Required'),
                    'name' => 'label4',
                    'class' => 't',
                    'multiple' => true,
                    'values' => array(
                        'query' => array($label3),
                        'id' => 'label4',
                        'name' => 'label4',
                        'expand' => array(
                            'default' => 'show',
                            'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'),
                            'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt')
                        ),
                    )
                ),


            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'name' => $this->l('submitAddproduct'),
            )
        ),
    );

    $helper = new HelperForm();
    $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;
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitUpdate';
    $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(
        $helper->fields_value['id_product'] = Tools::getValue('id_product'),
        'relab4' => 1,

        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id
    );

    $helper->fields_value['contenu1'] = $contenu1;
    $helper->fields_value['contenu2'] = $contenu2;
    $helper->fields_value['contenu3'] = $contenu3;
    $helper->fields_value['contenu4'] = $contenu4;
    $helper->fields_value['relab1'] = (int)$relab1;
    $helper->fields_value['relab2'] = (int)$relab2;
    $helper->fields_value['relab3'] = (int)$relab3;
    $helper->fields_value['relab4'] = (int)$relab4;
    $helper->fields_value['id_customization_field1'] = $id_customization_field1;
    $helper->fields_value['id_customization_field2'] = $id_customization_field2;
    $helper->fields_value['id_customization_field3'] = $id_customization_field3;
    $helper->fields_value['id_customization_field4'] = $id_customization_field4;


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

我猜您也有这部分代码(
$helper
是您的
HelperForm()
对象):

因此,使用此函数,您需要返回一个
relab2
值:

public function getConfigFieldsValues()
{
    return array(
        'relab2' => 1 // your value, 1 or 0
        // other form values
    );
}

您需要向
HelperForm
对象提供表单值

以下是一个例子:

$fields\u form=array(
“图例”=>数组(
“标题”=>$this->(“我的表格”),
),
'输入'=>数组(
'类型'=>'开关',
'label'=>$this->l('required'),
'name'=>'relab2',
'is_bool'=>是真的,
'desc'=>this->l('required'),
'值'=>数组(
排列(
'id'=>'label2_on',
“值”=>1,
'label'=>$this->l('Enabled')
),
排列(
'id'=>'label2_off',
“值”=>0,
'label'=>$this->l('Disabled')
)
)
),
);
$val=getValFromDB()//例子
$helper=新的HelperForm();
$helper->module=$this;
$helper->name_controller='example';
$helper->title=$this->displayName;
$helper->submit_action='example_action';
// [...]
// [...]
//这里你提供你的价值观
$helper->fields\u value=array('relab2'=>$val);
返回$helper->generateForm(数组(数组('form'=>$fields\u form));

大家好,我在prestashop 1.6和1.7上遇到了同样的问题,我最终解决了这个问题,所以我将与大家分享我的发现

这是我的开关:

 array(
    'type' => 'switch',
    'label' => $this->trans('Captcha Test Mode', array(), 'Admin.Shipping.Feature'),
     'name' => 'CORE_CAPTCHA_TEST_MODE',
     'required' => false,
     'class' => 't',
     'is_bool' => true,
      'values' => array(
            array(
                'id' => 'active_on',
                'value' => 1,
                'label' => $this->trans('Enabled', array(), 'Admin.Global'),
                 ),
            array(
                'id' => 'active_off',
                'value' => 0,
                'label' => $this->trans('Disabled', array(), 'Admin.Global'),
                 ),
       ),
正如您所看到的,它必须有一个明确的名称CORE\u CAPTCHA\u TEST\u MODE

有趣的是getConfigFormValues()方法:


与任何其他类型的字段不同,当为开关加载表单时,简单的配置::get足以在字段中设置值,我们需要添加Tools::getValue(…

也有同样的问题,机器人的问题在别处,只是开关按钮本身不起任何作用,您必须向其提供值数据,然后显示更改

如其他所述,开关输入本身:

         array(
          'type' => 'switch',
          'label' => $this->l('Display map'),
          'name' => 'my_data',
          'is_bool' => true,
          'values' => array(
              array(
                  'id' => 'label2_on',
                  'value' => 1,
                  'label' => $this->l('Enabled')
              ),
              array(
                  'id' => 'label2_off',
                  'value' => 0,
                  'label' => $this->l('Disabled')
              )
          )
      ),
并在下面提供保存的数据

    $helper->fields_value['my_data'] = Configuration::get('my_data');

那么,你能用你的全部代码编辑你的问题吗?谢谢你的时间,你可以看到整个脚本对不起我的错误和错误分配$relab vaiables谢谢你们的时间!
         array(
          'type' => 'switch',
          'label' => $this->l('Display map'),
          'name' => 'my_data',
          'is_bool' => true,
          'values' => array(
              array(
                  'id' => 'label2_on',
                  'value' => 1,
                  'label' => $this->l('Enabled')
              ),
              array(
                  'id' => 'label2_off',
                  'value' => 0,
                  'label' => $this->l('Disabled')
              )
          )
      ),
    $helper->fields_value['my_data'] = Configuration::get('my_data');