Prestashop - 使用帮助形式

时间:2015-05-22 07:19:33

标签: php prestashop prestashop-1.6

我正在创建一个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')),
            )
        ),
    )
);

3 个答案:

答案 0 :(得分:7)

以下是我在阅读 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
        ),
    )
);

答案 1 :(得分:0)

你可以用这个

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

答案 2 :(得分:0)

用于在init处修复不显示标签, 添加setFullTree(true)

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