prestashop模块配置页面中的categoryChoiceTree
categoryChoiceTree in prestashop module configuration page
我正在开发一个 prestashop 模块,我正试图在我的后台配置页面中显示一个类别树。
我正在尝试按照下面的说明进行操作,但我不知道确切的添加位置 this code.
应该在主模块的php里面?或者在一个单独的 .php 文件中并从主文件中调用它(也不知道该怎么做)。
我花了很多时间试图弄清楚如何实现上面 link 中的代码,但我觉得我在浪费时间。
我看到 "use" 个文件,而这个 JS“/admin-dev/themes/new-theme/js/components/form/choice-tree.js”不在任何 prestashop 文件夹中。
嗯,你应该花一些时间学习 Symfony,因为这是你为 Prestashop 1.7 构建后端模块所需要的。
作为一个指针,您需要创建一个表单 class 扩展 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'
])
然后添加用于检索数据以填充表单字段的方法。
然后在你的控制器中使用这个 class 并显示表格:
$form = $this->createForm(YourFormForm::class);
另外添加一个processForm来处理数据。
如前所述,这不是您需要了解 Symfony 工作流程的 copy/paste 情况。
我发现 "paint" 配置页面中类别树的唯一方法是将此代码添加到输入表单数组中:
谁能告诉我如何将用户选择数据检索到我的数据库中?
它不像任何其他表单域那样工作。
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
感谢您的宝贵时间,也感谢本论坛的另一位 post 的帮助。
$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
我正在开发一个 prestashop 模块,我正试图在我的后台配置页面中显示一个类别树。
我正在尝试按照下面的说明进行操作,但我不知道确切的添加位置 this code.
应该在主模块的php里面?或者在一个单独的 .php 文件中并从主文件中调用它(也不知道该怎么做)。
我花了很多时间试图弄清楚如何实现上面 link 中的代码,但我觉得我在浪费时间。 我看到 "use" 个文件,而这个 JS“/admin-dev/themes/new-theme/js/components/form/choice-tree.js”不在任何 prestashop 文件夹中。
嗯,你应该花一些时间学习 Symfony,因为这是你为 Prestashop 1.7 构建后端模块所需要的。
作为一个指针,您需要创建一个表单 class 扩展 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'
])
然后添加用于检索数据以填充表单字段的方法。
然后在你的控制器中使用这个 class 并显示表格:
$form = $this->createForm(YourFormForm::class);
另外添加一个processForm来处理数据。
如前所述,这不是您需要了解 Symfony 工作流程的 copy/paste 情况。
我发现 "paint" 配置页面中类别树的唯一方法是将此代码添加到输入表单数组中:
谁能告诉我如何将用户选择数据检索到我的数据库中? 它不像任何其他表单域那样工作。
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
感谢您的宝贵时间,也感谢本论坛的另一位 post 的帮助。
$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