问题描述
我希望能够在前面显示一些类别(我添加的名称和自定义字段)。我创建了一个自定义模块,可以在其中获取类别列表以将其显示在后台,这样我就可以选择一些在前端使用它们。
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/
- 类别类用于在数据库中存储任何类型的信息,它使用 ps_category 表(如果将ps_用作表前缀)。最常用的方法是 get(KEY)来检索信息,以及 updateValue(KEY)来将值存储在数据库中
- 工具类具有与请求和PS生态系统进行交互的全套方法,即 getValue(KEY)可从POST或GET请求中获取值(也上传文件等)或 isSubmit(KEY)来检查该值是否已提交(同样是从GET或POST提交)
类文件位于root / classes中,检查它们以发现所有方法?