Prestashop - 使用辅助表单添加多个类别树

Prestashop - Add multiple category trees with helper form

我正在创建一个需要 select 2 个类别根目录的 prestashop 模块。

我尝试添加 2 个类型为 "categories" 的字段,但在第二个类别树上,它与第一棵树具有相同的 ID 和相同的名称。

$fields_form[1]['form'] = array(
    'legend' => array(
        'title' => $this->l('Setting'),
    ),
    'input' => array(       
        array(
            'type'  => 'text',
            'label' => $this->l('First column title'),
            'name'  => 'HCA_TITLE_COL1',
        ),
        array(
           'type'  => 'categories',
           'label' => $this->l('Root category'),
           'desc'    => $this->l('Root category of the first column.'),  
           'name'  => 'HCA_CAT_COL1',
           'tree'  => array(
                'id' => 'HCA_CAT_COL1',
                'selected_categories' => array((int)Configuration::get('HCA_CAT_COL1')),
            )
        ),
        array(
            'type'  => 'text',
            'label' => $this->l('Second column title'),
            'name'  => 'HCA_TITLE_COL2',
        ),
        array(
           'type'  => 'categories',
           'label' => $this->l('Root category'),
           'desc'    => $this->l('Root category of the second column.'),  
           'name'  => 'HCA_CAT_COL2',
           'tree'  => array(
                'id' => 'HCA_CAT_COL2',
                'selected_categories' => array((int)Configuration::get('HCA_CAT_COL2')),
            )
        ),
    )
);

这是我阅读admin\themes\default\template\helpers\form\form.tpl

后找到的解决方案

在这个文件中,有这个条件

{elseif $input.type == 'categories_select'}
    {$input.category_tree}

因此,我使用 'type' = 'categories_select' 而不是 'type' = 'categories',并且我手动生成了类别树。

$root = Category::getRootCategory();

//Generating the tree for the first column
$tree = new HelperTreeCategories('categories_col1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
    ->setAttribute('is_category_filter', $root->id)
    ->setRootCategory($root->id)
    ->setSelectedCategories(array((int)Configuration::get('HCA_CAT_COL1')))
    ->setInputName('HCA_CAT_COL1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTreeCol1 = $tree->render();

//Generating the tree for the second column
$tree = new HelperTreeCategories('categories_col2');
$tree->setUseCheckBox(false)
     ->setAttribute('is_category_filter', $root->id)
     ->setRootCategory($root->id)
     ->setSelectedCategories(array((int)Configuration::get('HCA_CAT_COL2')))
     ->setInputName('HCA_CAT_COL2');
$categoryTreeCol2 = $tree->render();


$fields_form[1]['form'] = array(
    'legend' => array(
        'title' => $this->l('Setting'),
    ),
    'input' => array(       
        array(
            'type'  => 'text',
            'label' => $this->l('First column title'),
            'name'  => 'HCA_TITLE_COL1',
        ),
        array(
           'type'  => 'categories_select',
           'label' => $this->l('Root category'),
           'desc'    => $this->l('Root category of the first column.'),  
           'name'  => 'HCA_CAT_COL1',
           'category_tree'  => $categoryTreeCol1 //This is the category_tree called in form.tpl
        ),
        array(
            'type'  => 'text',
            'label' => $this->l('Second column title'),
            'name'  => 'HCA_TITLE_COL2',
        ),
        array(
           'type'  => 'categories_select',
           'label' => $this->l('Root category'),
           'desc'    => $this->l('Root category of the second column.'),  
           'name'  => 'HCA_CAT_COL2',
           'category_tree'  => $categoryTreeCol2
        ),
    )
);

你可以用这个

array(
    'type' => 'html',
...
    'html_content'  => $categoryTreeCol1

修复初始化时标签不显示的问题, 添加 setFullTree(true)

$tree->setUseCheckBox(true)
        ->setAttribute('is_category_filter', $root->id)
        ->setRootCategory($root->id)
        ->setFullTree(true)
        ->setSelectedCategories($selected_categories)
        ->setInputName('associated_categories');