doumi9618 2016-11-11 09:19
浏览 38
已采纳

从prestashop管理员控制器渲染帮助器表单

I am trying to add a helper form that lets the user upload images for two languages that the user can select.

However I am stuck with the form and cannot render it in the view. Here is my controller code:

<?php

class AdminWineoHeaderImgController extends ModuleAdminController
{
    public function __construct()
    {
        $this->bootstrap = true;
        $this->lang = (!isset($this->context->cookie) ||
         !is_object($this->context->cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($this->context->cookie->id_lang);

        parent::__construct();
    }

    public function display()
    {
        parent::display();
    }

    public function renderList()
    {
        $this->renderForm();
        $return = $this->context->smarty->fetch(_PS_MODULE_DIR_.'wineoheaderimg/views/templates/hook/adminwineoimg.tpl');

        return $return;
    }

    public function renderForm()
    {
        $fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->module->l('Wineo Header Img Configuration'),
                    'icon' => 'icon-envelope',
                ),
                'input' => array(
                    array(
                        'type' => 'file',
                        'label' => $this->module->l('Add images'),
                        'name' => 'enable_grades',
            'id' => 'uploadwineoheaderimg',
            'required' => false,
                        'desc' => $this->module->l('Choose images that will appear on the front page.'),
                    ),
                    array(
                    'type' => 'select',
                    'label' => $this->l('Languages:'),
                    'name' => 'category',
                    'required' => true,
                    'options' => array(
                                'query' => $options = array(
                                            array(
                                              'id_option' => 1,       // The value of the 'value' attribute of the <option> tag.
                                              'name' => 'EN',    // The value of the text content of the  <option> tag.
                                            ),
                                            array(
                                              'id_option' => 2,
                                              'name' => 'BG',
                                            ),
                                ),
                                'id' => 'id_option',
                                'name' => 'name',
                               ),
                ),
                ),
                'submit' => array('title' => $this->module->l('Save')),
            ),
        );

        $helper = new HelperForm();
        $helper->table = 'wineoheaderimg';
        $helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
        $helper->allow_employee_form_lang = (int) Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG');
        $helper->submit_action = 'wineo_header_img_pc_form';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->module->name.'&tab_module='.$this->module->tab.'&module_name='.$this->module->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = array(
            'fields_value' => array(
                'wineo_header_img' => Tools::getValue('enable_grades', Configuration::get('WINEO_HEADER_IMG')),
            ),
            'languages' => $this->context->controller->getLanguages(),
        );

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

Where should I call the method renderForm()? I have tried in the admin hooks and basically everywhere I could imagine.

Any help will be appreciated!

  • 写回答

1条回答 默认 最新

  • doushi7394 2016-11-11 15:35
    关注

    Well you are calling renderForm() inside renderList(), (I assume you want the form to display by default when you open controller page) but you don't assign the form to template.

    public function renderList()
    {
        $form = $this->renderForm();
    
        // To load form inside your template
        $this->context->smarty->assign('form_tpl', $form);
        return $this->context->smarty->fetch(_PS_MODULE_DIR_.'wineoheaderimg/views/templates/hook/adminwineoimg.tpl');
    
        // To return form html only
        return $form;
    }
    

    So if you want the form inside your adminwineoimg.tpl

    {* Some HTML *}
    {$form_tpl}
    {* Some HTML *}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B