如何在 1 个 admincontroller 中合并 fields_list + .tpl 视图?

How to combine a fields_list + .tpl view in 1 admincontroller?

我不知道如何在我的自定义管理控制器中显示 1 fields_list + .tpl 文件的内容。目标是在库存中显示我的产品密钥 + 下面的一些额外功能(来自 tpl 文件的内容)。

我可以显示字段列表 来自 .tpl 文件的消息。但没有合并...我在网上找到了一个教程,这非常接近,但没有用。

<?php

 require_once(_PS_MODULE_DIR_ . 'avanto_key/classes/AvantoStock.php');

class AdminAvantokeyStockController extends ModuleAdminController
{
    protected $position_identifier = 'id_avanto_keys';

    public function __construct()
    {
        //$this->fields_form = $this->fieldForm();
        $this->bootstrap = true;
        $this->table = 'avanto_keys'; // DB table name where your object data stored
        $this->className = "AvantoStock"; // The class name of my object
        //$this->identifier = 'id_avanto_keys';
        //$this->list_id = 'id_avanto_keys';
        $this->_defaultOrderBy = 'id_avanto_keys';
        //$this->lang = FALSE;

        $this->addRowAction('edit');
        $this->addRowAction('delete');

        $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
            'confirm' => $this->l('Delete selected items?')), );

        //Shop::addTableAssociation($this->table, array('type' => 'shop'));



    parent::__construct();
        $this->_select =  'pl.`name` as product_name, a.`serial_key` as serial_display';
        $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` 
        = '.(int)$this->context->language->id.')';
    }

    public function renderView()
    {
        $tpl = $this->context->smarty->createTemplate(
            dirname(__FILE__).
            '/../../views/templates/admin/view.tpl');
        return $tpl->fetch();
    }

    public function renderList()
    {
        $this->toolbar_title = $this->l('Stock Management');
        $this->toolbar_btn['new'] = null;
        $this->fields_list = array(
            'id_avanto_keys' => array(
                'title' => $this->l('ID Key'),
                'width' => 140,
            ),
            'id_product' => array(
                'title' => $this->l('Product ID'),
                'width' => 140,
            ),
            'serial_key' => array(
                'title' => $this->l('Serial Keys'),
                'width' => 140,
            ),
            'product_name' => array(
                'title' => $this->l('Product Name'),
                'width' => 140,
            ),
        );

        return parent::renderList();
    }


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

    public function initContent()
    {

        $this->context->smarty->assign(array(
            'form' => $form,
            'base_dir' =>  _PS_MODULE_DIR_,
        ));
        $this->setTemplate('stock.tpl');
        $lists = parent::initContent();


        $this->renderList();
        $lists .= parent::initContent();

        return $lists;
    }



    public function renderForm()
    {
        $this->display = 'edit';
        $this->initToolbar();

        $this->fields_form = array(
            'tinymce' => true,
            'legend' => array(
                'title' => $this->l('Edit product key'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Key ID'),
                    'name' => 'id_product',
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Product ID'),
                    'name' => 'id_avanto_keys',
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Serial Key'),
                    'required' => true,
                    'name' => 'serial_key',
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'btn btn-default pull-right'
            )
        );

        return parent::renderForm();
    }




}

?>  

上面的代码只显示消息 hello world 而不是我的产品列表。 有人知道如何结合吗?

提前致谢!!!

有点乱,得整理一下:):

第一个:
列表的字段最好在 __construct 中声明,因此:

public function __construct()
{
    $this->module = 'YourModuleName'; // Here you have to put your module name

    $this->bootstrap = true;
    $this->table = 'avanto_keys'; // DB table name where your object data stored
    $this->className = "AvantoStock"; // The class name of my object

    //$this->identifier = 'id_avanto_keys';
    //$this->list_id = 'id_avanto_keys';
    $this->_defaultOrderBy = 'id_avanto_keys';
    //$this->lang = FALSE;
    $this->explicitSelect = true; // This if you do a select manually after

    $this->addRowAction('edit');
    $this->addRowAction('delete');

    $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
        'confirm' => $this->l('Delete selected items?')), );

    //Shop::addTableAssociation($this->table, array('type' => 'shop'));


    $this->_select =  'pl.`name` as product_name, a.`serial_key` as serial_display';
    $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` 
    = '.(int)$this->context->language->id.')';

    $this->fields_list = array(
        'id_avanto_keys' => array(
            'title' => $this->l('ID Key'),
            'width' => 140,
        ),
        'id_product' => array(
            'title' => $this->l('Product ID'),
            'width' => 140,
        ),
        'serial_key' => array(
            'title' => $this->l('Serial Keys'),
            'width' => 140,
        ),
        'product_name' => array(
            'title' => $this->l('Product Name'),
            'width' => 140,
        ),
    );

    parent::__construct();
}

第二
renderList 方法制作其他内容,让我们将其与您要显示的内容分开:

public function renderList()
{
    // Here we retrieve the list (without doing any strange thing)
    $list = parent::renderList();

    // Assign some vars to pass to our custom tpl
    $this->context->smarty->assign(
        array( 
            'var1' => "Test",
            'var2' => "Test2"
            )
        );

    // Get the custom tpl rendered
    $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . "avanto_key/views/templates/admin/avantokeystock/customcontent.tpl");

    // return the list plus your content
    return $list . $content;
}

第三
让父initContent保持原样,不要覆盖,因为他做了很多东西

我想这是一个很好的起点:)
试试这个方法,让我知道 ;)