⚙️ HelperOptions — pagina de configuracion global
Actualizado: 2024-12-01
HelperOptions es una variante de HelperForm orientada a paginas de configuracion de AdminControllers del core (como AdminGeneralController). Genera bloques de opciones con guardado automatico en Configuration o propiedades del objeto.
#HelperOptions vs HelperForm
| Caracteristica | HelperForm | HelperOptions |
|---|---|---|
| Uso tipico | CRUD de entidades | Configuracion global |
| Estructura | fields_form[] | fields_options[] |
| Guardado | Manual en postProcess() | Automatico en AdminController |
| Agrupacion visual | Paneles (legend) | Tabs o paneles |
| Auto-populate | fields_value | Lee Configuration directamente |
#Estructura fields_options
Estructura basica de fields_options
php
<?php
// En un AdminController (extends AdminController)
public function __construct()
{
$this->table = 'configuration';
$this->identifier = 'id_configuration';
$this->className = 'Configuration';
$this->bootstrap = true;
$this->fields_options = [
// ── GRUPO 1 ──
'general' => [
'title' => $this->l('Configuracion General'),
'icon' => 'icon-cogs',
'fields' => [
'MY_MODULE_API_KEY' => [
'title' => $this->l('API Key'),
'hint' => $this->l('Clave de API del proveedor'),
'type' => 'text',
'size' => 60,
'required' => true,
'class' => 'fixed-width-xxl',
'validation' => 'isGenericName',
],
'MY_MODULE_ACTIVE' => [
'title' => $this->l('Activar servicio'),
'type' => 'bool',
'required' => false,
],
],
'submit' => ['title' => $this->l('Guardar')],
],
// ── GRUPO 2 ──
'advanced' => [
'title' => $this->l('Opciones Avanzadas'),
'icon' => 'icon-wrench',
'fields' => [
'MY_MODULE_MODE' => [
'title' => $this->l('Modo de operacion'),
'type' => 'select',
'list' => [
['id' => 'sandbox', 'name' => 'Sandbox (Pruebas)'],
['id' => 'production', 'name' => 'Produccion'],
],
'identifier' => 'id',
'validation' => 'isCleanHtml',
],
'MY_MODULE_TIMEOUT' => [
'title' => $this->l('Timeout (segundos)'),
'type' => 'text',
'size' => 5,
'validation' => 'isUnsignedInt',
'cast' => 'intval',
],
],
'submit' => ['title' => $this->l('Guardar opciones avanzadas')],
],
];
parent::__construct();
}
#Tipos de campos
| Tipo | Descripcion | Atributos especiales |
|---|---|---|
| text | Campo de texto | size, class, prefix, suffix, required |
| bool | Toggle si/no | - |
| select | Lista desplegable | list (array), identifier |
| textarea | Texto multilínea | cols, rows |
| color | Selector de color | - |
| file | Subida de archivo | display_image |
| password | Contraseña | - |
| checkbox | Casilla multiple | values (array) |
| radio | Botones de radio | values (array) |
| image | Mostrar imagen actual | name, dir |
| thumbnails | Galeria de thumbnails | files, name |
#Procesar y guardar
Procesar el submit en postProcess() del AdminController
php
<?php
// En el AdminController
public function postProcess(): void
{
// HelperOptions genera un submit_btn con nombre 'submitOptions{tabla}'
// Para tabla='configuration', es 'submitOptionsconfiguration'
if (Tools::isSubmit('submitOptionsconfiguration')) {
// Iterar los grupos y campos para guardar
foreach ($this->fields_options as $group) {
foreach ($group['fields'] as $key => $field) {
$value = Tools::getValue($key);
// Aplicar cast si existe
if (isset($field['cast'])) {
$value = $field['cast']($value);
}
// Validar si existe
if (isset($field['validation'])) {
$method = $field['validation'];
if (!Validate::$method($value)) {
$this->errors[] = $this->l('Valor invalido para: ') . $field['title'];
continue;
}
}
Configuration::updateValue($key, $value);
}
}
if (empty($this->errors)) {
$this->confirmations[] = $this->l('Configuracion guardada correctamente.');
}
}
parent::postProcess();
}
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.