Formularios de configuracion en modulos
Actualizado: 2024-12-01
Todos los modulos de PrestaShop pueden tener una pagina de configuracion accesible desde BO > Modulos > Configurar. Se implementa en el metodo getContent() del modulo. Hay dos enfoques: HelperForm (rapido, compatible PS 1.7+) o Symfony Form (moderno, PS 8+).
#Metodo getContent() — pagina de config del modulo
Estructura base de getContent()
php
<?php
/**
* Se ejecuta cuando el usuario hace clic en 'Configurar' en la lista de modulos.
* Debe devolver el HTML de la pagina de configuracion.
*/
public function getContent(): string
{
$output = '';
// 1. Procesar el formulario si se envio
if (Tools::isSubmit('submit' . $this->name)) {
$output .= $this->processConfigForm();
}
// 2. Renderizar el formulario con los valores actuales
$output .= $this->renderConfigForm();
return $output;
}
private function processConfigForm(): string
{
$errors = [];
$apiKey = trim(Tools::getValue('MYMODULE_API_KEY', ''));
$active = (int) Tools::getValue('MYMODULE_ACTIVE', 0);
$mode = Tools::getValue('MYMODULE_MODE', 'sandbox');
if (empty($apiKey)) {
$errors[] = $this->trans('La API Key es obligatoria.', [], 'Modules.Mymodule.Admin');
}
if (!empty($errors)) {
return $this->displayError(implode('<br>', $errors));
}
Configuration::updateValue('MYMODULE_API_KEY', $apiKey);
Configuration::updateValue('MYMODULE_ACTIVE', $active);
Configuration::updateValue('MYMODULE_MODE', pSQL($mode));
return $this->displayConfirmation(
$this->trans('Configuracion guardada.', [], 'Modules.Mymodule.Admin')
);
}
#HelperForm — opcion rapida
renderConfigForm() con HelperForm
php
<?php
private function renderConfigForm(): string
{
$helper = new HelperForm();
$helper->table = $this->name;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->submit_action = 'submit' . $this->name;
$helper->default_form_language = $this->context->language->id;
$helper->fields_value = [
'MYMODULE_API_KEY' => Configuration::get('MYMODULE_API_KEY', ''),
'MYMODULE_ACTIVE' => Configuration::get('MYMODULE_ACTIVE', false),
'MYMODULE_MODE' => Configuration::get('MYMODULE_MODE', 'sandbox'),
];
$form = [[
'form' => [
'legend' => [
'title' => $this->trans('Configuracion', [], 'Modules.Mymodule.Admin'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'text',
'label' => $this->trans('API Key', [], 'Modules.Mymodule.Admin'),
'name' => 'MYMODULE_API_KEY',
'required' => true,
],
[
'type' => 'switch',
'label' => $this->trans('Activo', [], 'Modules.Mymodule.Admin'),
'name' => 'MYMODULE_ACTIVE',
'values' => [
['id'=>'on', 'value'=>1, 'label'=>$this->trans('Si', [], 'Admin.Global')],
['id'=>'off', 'value'=>0, 'label'=>$this->trans('No', [], 'Admin.Global')],
],
],
[
'type' => 'select',
'label' => $this->trans('Modo', [], 'Modules.Mymodule.Admin'),
'name' => 'MYMODULE_MODE',
'options' => [
'query' => [
['id'=>'sandbox', 'name'=>'Sandbox'],
['id'=>'production', 'name'=>'Produccion'],
],
'id' => 'id',
'name' => 'name',
],
],
],
'submit' => ['title' => $this->trans('Guardar', [], 'Admin.Actions')],
],
]];
return $helper->generateForm($form);
}
#API Configuration — guardar ajustes
Metodos de la API Configuration para modulos
php
<?php
// ── LEER ──
$value = Configuration::get('MYMODULE_KEY'); // string o false
$value = Configuration::get('MYMODULE_KEY', 'default'); // con valor por defecto
$value = Configuration::get('MYMODULE_KEY', null, null, $idShop); // por tienda
// ── GUARDAR ──
Configuration::updateValue('MYMODULE_KEY', $value);
Configuration::updateValue('MYMODULE_KEY', $value, false, null, $idShop); // por tienda
// ── ELIMINAR (en uninstall()) ──
Configuration::deleteByName('MYMODULE_KEY');
// ── PATRONES RECOMENDADOS ──
// Prefija siempre con el nombre del modulo en mayusculas
// MYMODULE_SETTING en lugar de SETTING
// Lista de claves para limpiar en uninstall()
private array $configKeys = [
'MYMODULE_API_KEY',
'MYMODULE_ACTIVE',
'MYMODULE_MODE',
];
public function uninstall(): bool
{
foreach ($this->configKeys as $key) {
Configuration::deleteByName($key);
}
return parent::uninstall();
}
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.