Hello,
I’m trying to have a controller with a dynamic $listConfig variable depending on some context.
I tried something like this :
public $listConfig = $this->getListConfig()
with my logic in getListConfig, but I get an error :
type or paste code here
So I tried to define it in the constructor like this :
public $listconfig;
public function __construct() {
$this->listConfig = $this->getListConfig();
}
but I get this error :
Class Me\Mymodule\Controllers\Records must define the property $listConfig used by the behavior Backend\Behaviors\ListController.
I’m stuck here and don’t know what is the right way to have some logic to have a dynamic $listConfig
variable.
If you have some suggestions to give me
Best regards,
I finally found the solution, the first one works, you just have to define you listConfig in the constructor before calling parent::_construct. So I give something like this :
public $listConfig = [];
public function __construct() {
// Define dynamic $listConfig
$this->listConfig = $this->getListConfig();
parent::__construct();
...
daft
January 18, 2023, 11:07pm
#3
This also works. Override the method in your controller:
public function listGetConfig($definition)
{
$config = $this->asExtension('ListController')->listGetConfig($definition);
// ... Do stuff
return $config;
}
apinard
January 19, 2023, 1:19am
#4
As an addition…
If you are looking to change the formConfig, listConfig or relationConfig based on some conditions/roles, you can also do it in Plugin.php.
Example:
public function boot() {
AuthorName\PluginName\Controllers\ControllerName::extend(function($controller) {
if (!$controller instanceof \AuthorName\PluginName\Controllers\ControllerName) {
return;
}
$user = \BackendAuth::getUser();
$code = $user->role->code;
if ($code == 'super_user') {
$controller->formConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_form_super_user.yaml';
$controller->listConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_list_super_user.yaml';
$controller->relationConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_relation_super_user.yaml';
}
else {
$controller->formConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_form_regular.yaml';
$controller->listConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_list_regular.yaml';
$controller->relationConfig = '$/AuthorName/PluginName/Controllers/ControllerName/config_relation_regular.yaml';
}
});
}