📋 HelperList — tablas y listados legacy en el Admin

Actualizado: 2024-12-01

HelperList genera tablas de datos con paginacion, ordenacion, busqueda y acciones en el Back Office. Se usa tipicamente en AdminController o en la pagina de configuracion de un modulo para mostrar registros de una tabla propia.

#Estructura de HelperList

Ejemplo completo de HelperList en getContent()
php
<?php

private function renderList(): string
{
    $fields_list = [
        'id_mymodule_item' => [
            'title'   => $this->l('ID'),
            'align'   => 'center',
            'width'   => 50,
        ],
        'name' => [
            'title'   => $this->l('Nombre'),
            'width'   => 'auto',
        ],
        'active' => [
            'title'   => $this->l('Activo'),
            'active'  => 'status',
            'type'    => 'bool',
            'align'   => 'center',
            'width'   => 50,
        ],
        'date_add' => [
            'title'   => $this->l('Fecha'),
            'type'    => 'datetime',
            'width'   => 150,
        ],
    ];

    $helper = new HelperList();

    $helper->shopLinkType    = '';
    $helper->simple_header   = false;
    $helper->identifier      = 'id_mymodule_item';   // Campo PK
    $helper->table           = 'mymodule_item';       // Nombre de la tabla (sin prefijo)
    $helper->actions         = ['edit', 'delete'];    // Acciones por fila
    $helper->show_toolbar    = true;
    $helper->toolbar_scroll  = true;
    $helper->listTotal       = $this->getTotalItems();
    $helper->_defaultOrderBy = 'id_mymodule_item';
    $helper->token           = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex    = AdminController::$currentIndex . '&configure=' . $this->name;
    $helper->title           = $this->l('Mis elementos');

    $this->_list = $this->getItems();

    return $helper->generateList($this->_list, $fields_list);
}

private function getItems(): array
{
    return Db::getInstance()->executeS(
        'SELECT * FROM `' . _DB_PREFIX_ . 'mymodule_item` ORDER BY id_mymodule_item DESC'
    ) ?: [];
}

private function getTotalItems(): int
{
    return (int) Db::getInstance()->getValue(
        'SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'mymodule_item`'
    );
}

#Definicion de columnas

PropiedadTipoDescripcion
titlestringTitulo de la columna
typestringTipo: text, bool, datetime, price, image
alignstringAlineacion: left, center, right
widthint|stringAncho en px o 'auto'
activestringNombre de la accion de toggle (e.g. 'status')
callbackstringNombre de metodo para renderizado custom
orderbyboolPermitir ordenar por esta columna (default true)
searchboolMostrar campo de busqueda (default true)

#Acciones por fila (row actions)

Procesar acciones de fila en postProcess() de getContent()
php
<?php

public function getContent(): string
{
    $output = '';

    // Accion de editar
    if (Tools::isSubmit('editmymodule_item')) {
        $id = (int) Tools::getValue('id_mymodule_item');
        // Redirigir a formulario de edicion
    }

    // Accion de eliminar
    if (Tools::isSubmit('deletemymodule_item')) {
        $id = (int) Tools::getValue('id_mymodule_item');
        Db::getInstance()->delete('mymodule_item', 'id_mymodule_item = ' . $id);
        $output .= $this->displayConfirmation($this->l('Elemento eliminado.'));
    }

    // Accion de toggle activo/inactivo
    if (Tools::isSubmit('statusmymodule_item')) {
        $id     = (int) Tools::getValue('id_mymodule_item');
        $active = (int) Db::getInstance()->getValue(
            'SELECT active FROM `' . _DB_PREFIX_ . 'mymodule_item` WHERE id_mymodule_item = ' . $id
        );
        Db::getInstance()->update('mymodule_item', ['active' => !$active], 'id_mymodule_item = ' . $id);
    }

    return $output . $this->renderList() . $this->renderForm();
}

#Bulk actions (acciones masivas)

Añadir bulk actions al HelperList
php
<?php

// Dentro de renderList()
$helper->bulk_actions = [
    'delete' => [
        'text'    => $this->l('Eliminar seleccionados'),
        'icon'    => 'icon-trash',
        'confirm' => $this->l('¿Estas seguro de eliminar los elementos seleccionados?'),
    ],
    'enableSelection' => [
        'text' => $this->l('Activar seleccionados'),
        'icon' => 'icon-power-off text-success',
    ],
    'disableSelection' => [
        'text' => $this->l('Desactivar seleccionados'),
        'icon' => 'icon-power-off text-danger',
    ],
];

// Procesar bulk action en getContent()
if (Tools::isSubmit('submitBulkdeleteMymodule_item')) {
    $ids = Tools::getValue('mymodule_itemBox', []);
    foreach ($ids as $id) {
        Db::getInstance()->delete('mymodule_item', 'id_mymodule_item = ' . (int) $id);
    }
}
Descargar en Markdown Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.