当前位置: 首页 > 知识库问答 >
问题:

通过Prestashop 1.7模块在后台创建表单

许俊雅
2023-03-14

我做了一个模块来钩住后台办公室产品页面中的表单(带有钩子DisplayAdminproductExtra)。我如何创建一个表单与一些输入模块?

我认为可以通过{helper.tplfile}或{form_字段.twigfile}来完成。如果有人将此解释为一个演练,我相信它也会成为许多其他人的一个很好的参考。这是由PrestaShop模块生成器创建的代码

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class Myfirstmodule extends Module
{
    protected $config_form = false;

    public function __construct()
    {
        $this->name = 'myfirstmodule';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'parsa';
        $this->need_instance = 0;

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('new module');
        $this->description = $this->l('first module');

        $this->confirmUninstall = $this->l('Are you sure?');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install()
    {
        Configuration::updateValue('MYFIRSTMODULE_LIVE_MODE', false);

        return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('backOfficeHeader') &&
            $this->registerHook('displayAdminProductsExtra');
    }

    public function uninstall()
    {
        Configuration::deleteByName('MYFIRSTMODULE_LIVE_MODE');

        return parent::uninstall();
    }

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitMyfirstmoduleModule')) == true) {
            $this->postProcess();
        }

        $this->context->smarty->assign('module_dir', $this->_path);

        $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

        return $output . $this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    protected function renderForm()
    {
        $helper = new HelperForm();

        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->module = $this;
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitMyfirstmoduleModule';
        $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->getConfigFormValues(), /* Add values for your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs',
                ),
                'input' => array(
                    array(
                        'type' => 'switch',
                        'label' => $this->l('Live mode'),
                        'name' => 'MYFIRSTMODULE_LIVE_MODE',
                        'is_bool' => true,
                        'desc' => $this->l('Use this module in live mode'),
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => true,
                                'label' => $this->l('Enabled')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => false,
                                'label' => $this->l('Disabled')
                            )
                        ),
                    ),
                    array(
                        'col' => 3,
                        'type' => 'text',
                        'prefix' => '<i class="icon icon-envelope"></i>',
                        'desc' => $this->l('Enter a valid email address'),
                        'name' => 'MYFIRSTMODULE_ACCOUNT_EMAIL',
                        'label' => $this->l('Email'),
                    ),
                    array(
                        'type' => 'password',
                        'name' => 'MYFIRSTMODULE_ACCOUNT_PASSWORD',
                        'label' => $this->l('Password'),
                    ),
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        return array(
            'MYFIRSTMODULE_LIVE_MODE' => Configuration::get('MYFIRSTMODULE_LIVE_MODE', true),
            'MYFIRSTMODULE_ACCOUNT_EMAIL' => Configuration::get('MYFIRSTMODULE_ACCOUNT_EMAIL', 'contact@prestashop.com'),
            'MYFIRSTMODULE_ACCOUNT_PASSWORD' => Configuration::get('MYFIRSTMODULE_ACCOUNT_PASSWORD', null),
        );
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        $form_values = $this->getConfigFormValues();

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

    /**
     * Add the CSS & JavaScript files you want to be loaded in the BO.
     */
    public function hookBackOfficeHeader()
    {
        if (Tools::getValue('module_name') == $this->name) {
            $this->context->controller->addJS($this->_path . 'views/js/back.js');
            $this->context->controller->addCSS($this->_path . 'views/css/back.css');
        }
    }

    /**
     * Add the CSS & JavaScript files you want to be added on the FO.
     */
    public function hookHeader()
    {
        $this->context->controller->addJS($this->_path . '/views/js/front.js');
        $this->context->controller->addCSS($this->_path . '/views/css/front.css');
    }

    public function hookDisplayAdminProductsExtra()
    {
        /* Place your code here. */
    }
}

共有1个答案

佴飞驰
2023-03-14

欢迎访问堆栈溢出

目前,在PrestaShop 1.7产品页面上使用HelpPerform并不起作用,建议使用HTML标记,并使用actionProductSavehook从$\u POST获取表单的所有值

 类似资料:
  • 我已经创建了一个Prestashop Backoffice模块,但在单击菜单中的模块选项卡时,我发现错误:在此处输入图像描述404页面未找到? 我想在点击模块选项卡上创建一个模板,并在那里显示一些文本。

  • 在我们前面的例子中,我们开始看到了。 我们的根模块有一个组件,一个管道和一个服务,其唯一的目的是处理信用卡。 如果我们将这三个元素提取到自己的功能模块,然后将它们导入我们的根模块怎么办? 我们将这样做。第一步是创建两个文件夹以区分属于根模块的元素和属于要素模块的元素。 注意每个模块文件夹下的模块文件: app.module.ts 和 credit-card.module.ts.。让我们先关注后者。

  • 如果你已经有一个项目,你想在这个项目中创建一个 Module ,你可以选择 File --> New Module 。 这样,会在当前的目录下创建一个 Module ,而不是在一个新的窗口中,单独创建 Project 和 Module 。 之后,你就会看到下面的界面。 同样的道理,你可以设置项目类型,但是,由于你现在是创建一个 Module ,所以只能选择一种类型。 下方的4个选项代表着不同的意义

  • 我对prestashop 1.7中select2中的ajax有问题。当我尝试写一些东西时,调用是200,但我得到错误“控制器Psb2BAjaxModuleAdmin丢失或无效。” 我在模块modules/psb2b/src/Controller/psb2bajaxmoduledmincontroller中创建了用于测试的控制器。php 在admin目录admin*********/themes/d

  • 在本章中,我们将研究在Joomla中Creating Modules 。 模块是灵活且轻量级的扩展,可用于页面呈现。 创建模块 以下是在Joomla中创建模块的简单步骤。 Step (1) - 在Joomla → modules文件夹中创建一个名为mod_firstmodule文件夹。 Step (2) - 在mod_firstmodule文件夹中创建一个名为“helper.php”的文件。 此文

  • 问题内容: 我有一个dbf,我想使用VB6复制到新的mdb中。 以下是我的专长,可以轻松地创建新的mdb,但是,我认为我可以使用INTO进行Select查询,以使用数据创建新表。 请注意:我假设的是在运行SQL查询时创建了MSAccess表。 我在FROM子句中收到语法错误。我试图做的是操纵此sql查询以执行我需要的操作: sql =“将INERT插入[Table1] SELECT * FROM