使用设置页面创建 Prestashop 后台模块
Creating Prestashop back-office module with settings page
我正在为 Prestashop 创建一个后台模块,并且已经找到了除显示管理页面的最佳方式之外的所有方法。目前我正在使用 renderView()
方法来显示 view.tpl.
的内容
我想显示一个 table,其中包含值和添加新行的选项。我应该只在 view.tpl 中创建它还是有更好的方法?我看过 renderForm()
方法,但还没有弄清楚它是如何工作的。
我遇到的最大问题是,如何将内容提交回我的控制器到特定方法?
ModuleAdminController
用于管理某种记录,即 ObjectModel
。此控制器的默认页面是一个列表,然后您可以单独编辑每条记录或查看其完整数据 (view
)。
如果你想要一个设置页面,最好的方法是为你的模块创建一个getContent()
函数。此外,对于此模块配置页面,HelperOptions
优于 HelperForm
,因为它会自动加载值。在此函数中定义表单,并在其上方添加一个 if (Tools::isSubmit('submit'.$this->name))
- 提交按钮 name
,然后将您的值保存到 configuration
table 中。 Configuration::set(...)
.
当然可以在 AdminController
中创建某种设置页面,但这并不意味着。如果你真的想:找到 HookCore.php
并找到 exec
方法。然后加上error_log($hook_name)
,你就会open/save/close一个page/form时执行的所有钩子。也许你会这样找到你的钩子。更好的方法是检查父 class AdminControllerCore
甚至 ControllerCore
。他们通常有特定的功能可以被覆盖,你应该把你的东西保存在那里。它们已经是执行过程的一部分,但是是空的。
编辑:您应该看看其他 AdminController
classes,它们非常简单;您只需要定义一些属性即可使其工作:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
其他人喜欢将列表和表单定义移动到呈现它们的实际函数中:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
您实际上不需要做任何其他事情,控制器将字段与您的模型匹配,加载它们,保存它们等。
同样,了解这些控制器的最佳方法是查看其他 AdminController。
我正在为 Prestashop 创建一个后台模块,并且已经找到了除显示管理页面的最佳方式之外的所有方法。目前我正在使用 renderView()
方法来显示 view.tpl.
我想显示一个 table,其中包含值和添加新行的选项。我应该只在 view.tpl 中创建它还是有更好的方法?我看过 renderForm()
方法,但还没有弄清楚它是如何工作的。
我遇到的最大问题是,如何将内容提交回我的控制器到特定方法?
ModuleAdminController
用于管理某种记录,即 ObjectModel
。此控制器的默认页面是一个列表,然后您可以单独编辑每条记录或查看其完整数据 (view
)。
如果你想要一个设置页面,最好的方法是为你的模块创建一个getContent()
函数。此外,对于此模块配置页面,HelperOptions
优于 HelperForm
,因为它会自动加载值。在此函数中定义表单,并在其上方添加一个 if (Tools::isSubmit('submit'.$this->name))
- 提交按钮 name
,然后将您的值保存到 configuration
table 中。 Configuration::set(...)
.
当然可以在 AdminController
中创建某种设置页面,但这并不意味着。如果你真的想:找到 HookCore.php
并找到 exec
方法。然后加上error_log($hook_name)
,你就会open/save/close一个page/form时执行的所有钩子。也许你会这样找到你的钩子。更好的方法是检查父 class AdminControllerCore
甚至 ControllerCore
。他们通常有特定的功能可以被覆盖,你应该把你的东西保存在那里。它们已经是执行过程的一部分,但是是空的。
编辑:您应该看看其他 AdminController
classes,它们非常简单;您只需要定义一些属性即可使其工作:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
其他人喜欢将列表和表单定义移动到呈现它们的实际函数中:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
您实际上不需要做任何其他事情,控制器将字段与您的模型匹配,加载它们,保存它们等。
同样,了解这些控制器的最佳方法是查看其他 AdminController。