在后端添加自定义模块选项卡

add custom module tab in backend

我创建了一个自定义模块:

modules/mymodule/mymodule.php

<?php

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

class mymodule extends Module
{
    public function __construct()
    {
        $this->name = 'mymodule';
        $this->tab = 'quick_bulk_update';
        $this->version = '1.0.0';
        $this->author = 'MYMODULE';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('MYMODULE');
        $this->description = $this->l('MYMODULE DESCRIPTION');

        $this->confirmUninstall = $this->l('¿Está seguro de desinstalar este módulo?');

        if (!Configuration::get('mymodule'))
            $this->warning = $this->l('No se ha introducido ningún nombre');
    }

    public function install()
    {
        if (Shop::isFeatureActive())
            Shop::setContext(Shop::CONTEXT_ALL);

        if (!parent::install() || !Configuration::updateValue('mymodule', 'mymodule'))
            return false;
        return true;
    }

    public function uninstall()
    {
        if (!parent::uninstall() ||
            !Configuration::deleteByName('mymodule')
        )
            return false;

        return true;
    }

    public function getContent()
    {
        $output = null;

        if (Tools::isSubmit('submit'.$this->name))
        {
            $my_module_name = strval(Tools::getValue('mymodule'));
            if (!$my_module_name
                || empty($my_module_name)
                || !Validate::isGenericName($my_module_name))
                $output .= $this->displayError($this->l('Valor de configuración incorrecto'));
            else
            {
                Configuration::updateValue('mymodule', $my_module_name);
                $output .= $this->displayConfirmation($this->l('Ajustes actualizados correctamente'));
            }
        }
        return $output.$this->displayForm();
    }

    public function displayForm()
    {
        // Get default language
        $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

        // Init Fields form array
        $fields_form[0]['form'] = array(
            'legend' => array(
                'title' => $this->l('Ajustes'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Valor de configuración'),
                    'name' => 'mymodule',
                    'size' => 20,
                    'required' => true
                )
            ),
            'submit' => array(
                'title' => $this->l('Guardar'),
                'class' => 'button'
            )
        );

        $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 = $default_lang;
        $helper->allow_employee_form_lang = $default_lang;

        // 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 = array(
            'save' =>
                array(
                    'desc' => $this->l('Guardar'),
                    'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                        '&token='.Tools::getAdminTokenLite('AdminModules'),
                ),
            'back' => array(
                'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'desc' => $this->l('Back to list')
            )
        );

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

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

}

我在后端添加了一个选项卡,也用于配置值。我是通过管理 > 后端选项菜单完成的。我之前创建了下一个文件:

modules/mymodule/controllers/admin/AdminMyModule.php

<?php
class AdminMyModuleController extends ModuleAdminController {

    public function __construct(){

    }
}

并且名为 mymodule 的选项卡在后台菜单中正确显示,但是当我 select 它时,加载的页面没有显示任何内容。 我需要显示上面displayForm函数的形式。当我转到模块 > mymodule > 配置时会显示此表单。而这种形式保存的值在ps_configurationtable中。 事实上,我只需要这个表格,在配置中保存一些值table,等等。

最后我是这样解决的:

class AdminMyModuleController extends ModuleAdminController {

public function __construct()
{
$this->bootstrap = true;
$this->className = 'Configuration';
$this->table = 'configuration';

parent::__construct();

$this->fields_options = array(
'general' => array(
'title' => $this->l('Ajustes para configurar mi modulo'),
'fields' => array(
'MYMODULE_IDCLIENTE' => array(
'title' => $this->l('Identificador de cliente'),
'type' => 'text',
'size' => 20,
'required' => true
),
'MYMODULE_EXECUTION_INTERVAL_UNIT' => array(
'title' => $this->l('Intervalo de ejecución (unidad)'),
'type' => 'select',
'size' => 5,
'required' => true,
'identifier' => "execution_interval_unit",
"desc" => "Indique si desea que la sincronización se realice cada x minutos, horas, días, semanas o meses.",
'list' => array(
array( "execution_interval_unit" => "1", "name" => 'Minutos' ),
array( "execution_interval_unit" => "2", "name" => 'Horas' ),
array( "execution_interval_unit" => "3", "name" => 'Días' ),
array( "execution_interval_unit" => "4", "name" => 'Semanas' ),
array( "execution_interval_unit" => "5", "name" => 'Meses' )
)
),
'MYMODULE_EXECUTION_INTERVAL_VALUE' => array(
'title' => $this->l('Intervalo de ejecución (valor)'),
'type' => 'text',
'size' => 20,
'required' => true
),
),
'submit' => array('title' => $this->l('Save'))
),
);
}
}

希望这对其他人有所帮助。 问候,