module - 特色类别 PrestaShop 1.7
问题描述
我希望能够在前面显示几个类别(名称和我添加的自定义字段)。我创建了一个自定义模块,在其中获取类别列表以在后台显示它,这样我就可以选择一些在前台使用它们。
public function getContent()
{
if (((bool) Tools::isSubmit('submit_feaduredcategoriesModule')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
return $output . $this->renderForm();
}
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submit_featuredcategoriesModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
. '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'categories',
'label' => $this->l('Featured categories'),
'name' => 'FEATURED_CATEGORIES',
'tree' => array(
'id' => 'category',
'selected_categories' => array((int)Configuration::get('category')),
'use_checkbox' => true
)
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
但是从这里我不确定在postProcess或getConfigFormValues中放入什么,我也不是getConfigForm中的“selected_categories” :(
任何帮助、提示、建议将不胜感激!提前致谢
解决方案
这两种方法postProcess和getConfigFormValues被用作事实上的标准,但不是强制性的。
顺便说一句,让我们从getConfigFormValues开始:
此方法应返回表单输入值的数组,索引作为输入的名称和输入期望的值,在您的情况下应该是这样的:
protected function getConfigFormValues()
{
return [
'FEATURED_CATEGORIES' => Category::getCategories() // Check the right method to use in the Category class
];
}
postProcess方法用于在提交表单(或多个表单)时执行所有操作。在你的情况下应该是这样的:
protected function postProcess()
{
if(Tools::isSumbit('submit_featuredcategoriesModule')) // Check if the form is submitted by checking the input name that you specify in $helper->submit_action
{
// Do your stuff
}
}
提示:
- 使用官方的devdocs,信息量真的很大:https ://devdocs.prestashop.com/
- Category类用于在数据库中存储任何类型的信息,它使用ps_category表(如果 ps_ 用作表前缀)。最常用的方法是get(KEY)检索信息,updateValue(KEY)将值存储在数据库中
- Tools类具有与请求和 PS 生态系统交互的全套方法,即getValue(KEY)从 POST 或 GET 请求(也上传文件等)获取值或isSubmit(KEY)以检查该值是否已提交(也来自 GET 或 POST)
类文件在 root/classes 中,检查它们以发现所有方法
推荐阅读
- reactjs - 如何在reactjs中编辑文本字段值
- ios - 有没有办法更快地获取更新的联系人?
- hyperledger-fabric - 无法在 Hyperledger Fabric 中启动 Peers?
- java - Akka Http 解析实体 Java
- vb.net - 绘制位图时处理内存堆叠
- python - 从图像本地目录创建 tensorflow 数据集
- ruby-on-rails - ruby on rails 应用程序卡在 futex 'kill QUIT PID' 但乘客没有留下任何痕迹
- linux - 如何从模块代码中设置 linux 内核模块名称?
- ruby-on-rails - 复选框 - 不更改布尔值
- node.js - 如何在无服务器设置中可靠地连接到 Mongodb?