Tutorial to migrate simple Legacy ModuleAdminControllers to Symfony
It's such a simple task to make a good looking, and well functioning module configuration form and/or a list of entities by just extending the Legacy ModuleAdminController. Below is a simple example on a module and it's admin-controller, that both list some table and creates a simple configuration form (that automatically gets the configuration values on load and saves them on submit). Easy peasy, and we love it.
// mymodule/mymodule.php
class mymodule extends Module {
public function __construct() {
// ...
$this->tabs = [
[
'class_name' => 'AdminMyModuleConfigController',
'parent_class_name' => 'AdminCatalog',
'name' => 'My Module settings',
'visible' => true,
],
];
}
}
// mymodule/controllers/admin/AdminMyModuleConfigController.php
class AdminMyModuleConfigController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
$this->bootstrap = true;
$this->table = $this->module->name.'_mylist';
$this->identifier = 'id';
$this->_defaultOrderBy = 'id';
$this->_defaultOrderWay = 'DESC';
$this->list_no_link = true;
$this->actions = [];
$this->actions_available = [];
$this->lang = false;
$this->list_simple_header = true;
$this->fields_list = [
'message' => [
'title' => $this->l('Messages'),
]
];
$this->fields_options = [
'options' =>
[
'title' => $this->l('My simple options'),
'fields' => [
$this->module->name.'_api_key' => [
'title' => $this->l('My API KEY'),
'type' => 'text',
],
'submit' => [
'title' => $this->l('Save'),
'name' => 'submitOptions'
]
]
]
];
}
}
I know that there does not exist such a simple way to do modern controllers, and that there are reasons to this (generating html-code directly in php etc). However: I hope that you will consider to:
- make it (almost) as easy to do very common things like the example above in new symfony module admin controllers. Off course, I think most of us would like an even more flexible system than the
HelperOptions,HelperFormandHelperList, and can handle a little bit more complexity in favour of getting a more flexible system. - that a tutorial on how to best/fastest migrate such simple legacy admin controllers into symfony.
I realise that this feature request is both relevant to the documentation and the code - let me know if you want me to move it.
Hi @onklmaps
I think nobody can disagree with what you say 😄 indeed this way of doing things is easy/peasy.
I think could be done by providing Helpers.
We dont want to re-implement the multiple $this-> properties available for legacy modules. This makes Module class a God Object and it comes with a lot of unforeseen bad consequences that can hit you at the most unexpected moment.
But we can provide helpers that would generate the right classes for you.
For example in modern controller we want you to use a Symfony form instead of $this->fields_options. So you need to do $form = new MyForm(...) and then configure it, build it, fill it with data then render it.
We could create a ModuleFormHelper that you can feed with the data you're used to, and it generates the form you are looking for. This being done, we avoid a God Object Module class but keep the simplicity.
These are just ideas in my head 😄 I'm not talking about final statements here
Hi @matks 🙂
Yes (!) Symfony form is the way to go, but maybe another (optional) layer on top of that to make (the most common cases) it simple to implement. Things like getting the configuration values from database automatically, and also providing an automatic postProcess of some sort.
Symfony forms are not hard to understand though, so maybe just using the standards here is simple (and powerful) enough, if a documentation and examples are provided to go with it 😉 How about Migrate Legacy admin controllers with HelperList to new Symfony controllers and Migrate Legacy admin controllers with HelperForm / HelperOptions to new Symfony controllers ?
Linked to https://github.com/PrestaShop/PrestaShop/issues/13701