【问题标题】:categoryChoiceTree in prestashop module configuration pageprestashop 模块配置页面中的 categoryChoiceTree
【发布时间】:2020-05-30 18:40:34
【问题描述】:

我正在开发一个 prestashop 模块,并试图在我的后台配置页面中显示一个类别树。

我正在尝试按照下面的说明进行操作,但我不知道在哪里添加this code.

它应该在主模块的php里面?或者在一个单独的 .php 文件中并从主文件中调用它(也不知道该怎么做)。

我花了很多时间试图弄清楚如何实现上面链接中的代码,但我觉得我浪费的时间越多。 我看到“使用”文件,而这个 JS,“/admin-dev/themes/new-theme/js/components/form/choice-tree.js”不在任何 prestashop 文件夹中。

【问题讨论】:

    标签: php module tree prestashop


    【解决方案1】:

    好吧,你应该花点时间学习一下 Symfony,因为这是为 Prestashop 1.7 构建后端模块所需要的。

    作为指针,你需要创建一个扩展CommonAbstractType的表单类,添加一个构建表单方法。例如:

    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'
            ])
    

    然后添加检索数据的方法以填充表单字段。

    然后在你的控制器中使用这个类并显示表单:

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

    还要添加一个 processForm 来处理数据。

    如上所述,这不是您需要了解 Symfony 工作流程的复制/粘贴情况。

    【讨论】:

    • 嗨 Robertino,这就是我实际在做的事情,但是如果你是菜鸟,我发现 symfony 和 prestashop 后台之间存在一些难以理解的差异,
    • 嗨,Pol,我理解,在开始使用新技术时感到沮丧是很正常的,这种情况发生在我们所有人身上。但是,我上面举例说明的内容适用于使用 Symfony 的 PS1.7.6+ 后端模块。
    【解决方案2】:

    我发现在配置页面中“绘制”类别树的唯一方法是将此代码添加到输入表单数组中:

    谁能告诉我如何将用户选择数据检索到我的数据库中? 它不能像任何其他表单域一样工作。

    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
                            ),
    

    【讨论】:

      【解决方案3】:

      嗯,解决了!!!!最后它非常简单,但您必须为您的特定情况获取正确的信息。 @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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-02
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多