使用prestashop中的自定义模块在客户视图树枝上添加自定义按钮?

时间:2019-10-12 09:36:44

标签: symfony twig prestashop symfony4 prestashop-1.7

首先,我尝试了与此主题相关的所有问题和答案。另外,我尝试了相关问题并尝试解决,但没有成功。因此,请仔细阅读我的问题。

我想使用我的“自定义”模块在prestashop(版本1.7.6.1)的“客户视图”页面上添加自定义按钮。

我找到了树枝文件:“ personal_information.html.twig”

文件位于:“ prestashop / src / prestashopModule / Resources / views / Admin / shell / Customer / view / personal_information.html.twig”

我附上我想放按钮的屏幕简短提示。

enter image description here

自定义模块[myModule]

<?php


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

class MyCustomModule extends  PaymentModule
{
    public function __construct()
    {
        $this->name = 'MyCustomModule';
        $this->tab = 'payments XYZ';
        $this->version                = '1.0';
        $this->author                 = 'XYZ Technologies';
        $this->bootstrap              = true;
        $this->displayName            = 'XYZ';
        $this->description            = 'XYZ.';
        $this->confirmUninstall       = 'Are you sure you want to uninstall XYZ module?';
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);
        $this->allow_countries        = array('CH', 'LI', 'AT', 'DE');
        $this->allow_currencies       = array('CHF', 'EUR');

        parent::__construct();

    }

    /**
    * Install this module and register the following Hooks:
    *
    * @return bool
    */
    public function install()
    {
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        Db::getInstance()->execute('
                CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'MyCustomModule` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `customer_id` int(255) NOT NULL,
                `MyCustomModule` int(255) DEFAULT NULL,
                `lastcheck_date` date,
                `add_date` date,
                PRIMARY KEY (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
        ');

        return parent::install() && $this->registerHook('Statusbtnoncustomerview');
    }
    /**
     * Uninstall this module and remove it from all hooks
     *
     * @return bool
     */
    public function uninstall()
    {
        return parent::uninstall() && $this->uninstallDb() && $this->unregisterHook('Statusbtnoncustomerview');
    }

    public function uninstallDb()
    {
        return Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'MyCustomModule');
    }

    public function hookStatusbtnoncustomerview()
    {
        /**
         * Verify if this module is enabled
         */
        if (!$this->active) {
            return;
        }
        return $this->fetch('module:MyCustomModule/views/templates/hook/personal_information.html.twig');
    }
    /**
     * Returns a string containing the HTML necessary to
     * generate a configuration screen on the admin
     *
     * @return string
     */
    public function getContent()
    {
        $output = null;
        if (Tools::isSubmit('submit'.$this->name)) {

            // get configuration fields value
            $MyCustomModule_Account_Data = strval(Tools::getValue('MyCustomModule_Account_Data'));
            $credit_Checkbox =  strval(Tools::getValue('credit_Checkbox_1'));
            $interval_Month = strval(Tools::getValue('Interval_Month'));

            if (
                !$MyCustomModule_Account_Data ||
                empty($MyCustomModule_Account_Data) ||
                !Validate::isGenericName($MyCustomModule_Account_Data)
            ) {
                $output .= $this->displayError($this->l('Please Enter MyCustomModule Account Data.'));
            } else{

                // Update configuration fields value
                Configuration::updateValue('MyCustomModule_Account_Data', $MyCustomModule_Account_Data);
                Configuration::updateValue('credit_Checkbox_1', $credit_Checkbox);
                Configuration::updateValue('Interval_Month', $interval_Month);


                // Display message after successfully submit value
                $output .= $this->displayConfirmation($this->l('Settings updated'));
            }
        }

        return $output.$this->displayForm();
    }
    /**
     * Display a form
     *
     * @param array $params
     * @return form html using helper form
     */
    public function displayForm()
    {
        // Get default language
        $defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
        $credit_Checkbox = [
            [
                'id'=>1,
                'name'=>'',
                'val' => 1
            ]
        ];
        // Init Fields form array
        $fieldsForm[0]['form'] = [
            'legend' => [
                'title' => $this->l('Configuration'),
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->l('MyCustomModule Account Data'),
                    'name' => 'MyCustomModule_Account_Data',
                    'required' => true
                ],
                [

                    'type'=>'checkbox',
                    'label'=> $this->l('credit'),
                    'name'=>'credit_Checkbox',
                    'values'=>[
                        'query'=>$credit_Checkbox,
                        'id'=>'id',
                        'name'=>'name'
                    ]

                ],
                [
                    'type' => 'html',
                    'html_content' => '<input type="number" min="0" step="1" value="'.Configuration::get('Interval_Month').'" name="Interval_Month">',
                    'label' => $this->l('interval Month'),
                    'name' => 'Interval_Month',
                    'size' => 20
                ],
            ],
            'submit' => [
                'title' => $this->l('Save'),
                'class' => 'btn btn-default pull-right'
            ]
        ];


        $helper = new HelperForm();

        // Module, token and currentIndex
        $helper->module = $this;
        $helper->name_controller = $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

        // Language
        $helper->default_form_language = $defaultLang;
        $helper->allow_employee_form_lang = $defaultLang;

        // Title and toolbar
        $helper->title = $this->displayName;
        $helper->show_toolbar = true;        // false -> remove toolbar
        $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
        $helper->submit_action = 'submit'.$this->name;
        $helper->toolbar_btn = [
            'save' => [
                'desc' => $this->l('Save'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
            ],
            'back' => [
                'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'desc' => $this->l('Back to list')
            ]
        ];

        // Load current value
        $helper->fields_value['MyCustomModule_Account_Data'] = Configuration::get('MyCustomModule_Account_Data');
        $helper->fields_value['credit_Checkbox_1'] = Configuration::get('credit_Checkbox_1');
        $helper->fields_value['Interval_Month'] = Configuration::get('Interval_Month');


        return $helper->generateForm($fieldsForm);
    }


}

1 个答案:

答案 0 :(得分:1)

从Prestashop 1.7开始,您可以在自己的模块中覆盖“后台”视图。

这意味着您只需要创建:

modules/mycustommodule/Admin/shell/Customer/view/personal_information.html.twig

然后,您应该扩展原始模板以正确覆盖它。您可以复制整个模板,然后添加按钮所需的html。希望它可以帮助您在此处找到所有文档:https://devdocs.prestashop.com/1.7/modules/concepts/templating/admin-views/