簡體   English   中英

prestashop 模塊配置頁面中的 categoryChoiceTree

[英]categoryChoiceTree in prestashop module configuration page

我正在開發一個 prestashop 模塊,並試圖在我的后台配置頁面中顯示一個類別樹。

我正在嘗試按照下面的說明進行操作,但我不知道在哪里添加此代碼。

它應該在主模塊的 php 里面嗎? 或在單獨的.php 文件中並從主文件中調用它(也不知道該怎么做)。

我花了很多時間試圖弄清楚如何實現上面鏈接中的代碼,我認為我浪費的時間越多。 我看到“使用”文件和這個 JS,“/admin-dev/themes/new-theme/js/components/form/choice-tree.js”不在任何 prestashop 文件夾中。

好吧,您應該花一些時間學習 Symfony,因為這是為 Prestashop 1.7 構建后端模塊所需要的。

作為指針,您需要創建一個擴展 CommonAbstractType 的表單 class,添加一個構建表單方法。 例如:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->context = Context::getContext();
        $parents = [
          ['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
        ];

        $builder->add('category', CategoryChoiceTreeType::class, [
            'choices_tree' => $parents,
            'choice_value' => 'id_category',
            'choice_children' => 'children',
            'choice_label' => 'name',
            'disabled_values' => $disabledCategories,
            'label' => 'Choose a category'
        ])

然后添加用於檢索數據的方法以填充表單字段。

然后在你的 controller 中使用這個 class 並顯示表格:

$form = $this->createForm(YourFormForm::class);

還要添加一個 processForm 來處理數據。

如前所述,這不是您需要了解 Symfony 工作流程的復制/粘貼情況。

我發現在配置頁面中“繪制”類別樹的唯一方法是將此代碼添加到輸入表單數組中:

誰能告訴我如何將用戶選擇數據檢索到我的數據庫中? 它不能像任何其他表單域一樣工作。

array(
                            'type' => 'categories',
                            'label' => $this->l('Destination Category'),
                            'desc' => $this->l('Select ONE Category'),
                            'name' => 'CATEGORY_CATEGORY_TO',
                            'tree' => [
                                // 'selected_categories' => [],
                                'disabled_categories' => null,
                                'use_search' => false,
                                'use_checkbox' => false,
                                'id' => 'id_category_tree',
                            ],
                            'required' => true
                        ),

好吧,它已解決,.,, 最后它非常簡單,但您必須為您的特定情況獲取正確的信息。 @Robertino 的答案可能是最好的實現,我不知道,但我無法解決,

我在下面使用此代碼,並從表單輸入中調用 $categoryTree。 此輸入必須是 type=> categories_select

感謝您抽出寶貴時間,並感謝本論壇另一篇文章的幫助。

$root = Category::getRootCategory();

    //Generating the tree
    $tree = new HelperTreeCategories('categories_1'); //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('CATEGORY_1'))) //if you wanted to be pre-carged
        ->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
    $categoryTree = $tree->render();

和形式:

                    array(
                    'type'  => 'categories_select',
                    'label' => $this->l('Category'),
                    'desc' => $this->l('Select Category '),
                    'name'  => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
                    'category_tree'  => $categoryTree, //This is the category_tree called in form.tpl
                    'required' => true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM