Search by

austral / list-bundle

Matthieu Beurelaustral

List mapper for Austral

Package info

github.com/austral-project/list-bundle

Homepage

Type:symfony-bundle

pkg:composer/austral/list-bundle

Statistics

Installs: 3 004

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v3.1.2 2024-07-19 11:39 UTC

This package is auto-updated.

Last update: 2026-11-25 14:31:53 UTC


README

Version CI PHP Symfony License

List mapper for Austral

This bundle is part of Austral, a modular back office and CMS for Symfony.

list-bundle describes a list of items with a fluent API (columns, row actions, global actions, children) and builds its view contract: plain arrays that a theme renders as a <table> or as nested <ol> lists. A minimal theme is provided; the admin theme is in admin-bundle / design-bundle.

list-bundle does not load the items: you give it the items (one page when paginated) and, optionally, a Pagination. Queries, filters, sections and titles belong to the code that uses the list (admin-bundle for the back office).

Requirements

  • PHP 8.4 or higher
  • Symfony 7.4

Installation

composer require austral/list-bundle

If you do not use Symfony Flex, register the bundle in config/bundles.php:

return [
    // ...
    Austral\ListBundle\AustralListBundle::class => ['all' => true],
];

The bundle has no configuration.

Usage

Describe the list

use Austral\ListBundle\Action\Action;
use Austral\ListBundle\Column;
use Austral\ListBundle\Mapper\ListMapper;

$mapper = $listMapperFactory->create('pages', ['translation_domain' => 'austral'], function (ListMapper $mapper): void {
    $mapper
        ->setLayout(ListMapper::LAYOUT_LIST)
        ->add(new Column\TemplateColumn('picto', ['label' => false, 'template' => 'admin/page/picto.html.twig']))
        ->add(new Column\ValueColumn('name', ['label' => 'form.labels.title', 'class' => 'flex-1']))
        ->add(new Column\DateColumn('updated', ['date_format' => 'd/m/Y']))
        ->add(new Column\SwitchColumn('isEnabled', [
            'url' => fn (Page $page, int $value): string => $this->urlGenerator->generate('page_change', ['id' => $page->getId(), 'value' => $value]),
            'granted' => $this->security->isGranted('ROLE_EDITOR'),
        ]))
        ->children(fn (Page $page): iterable => $page->getChildren())
        ->setRowUrl(fn (Page $page): string => $this->urlGenerator->generate('page_edit', ['id' => $page->getId()]))
        ->addRowAction(new Action('edit', [
            'url' => fn (Page $page): string => $this->urlGenerator->generate('page_edit', ['id' => $page->getId()]),
            'picto' => 'austral-picto-edit',
        ]), 10)
        ->addAction(new Action('create', ['url' => $this->urlGenerator->generate('page_create')]), 10);
});

The factory (austral.list.mapper_factory) dispatches ListEvents::CONFIGURE_BEFORE, calls the closure, then dispatches ListEvents::CONFIGURE_AFTER: a bundle can add or change columns and actions of every list.

Build and render

$pagination = new Pagination($request->query->getInt('page', 1), 20, $total, fn (int $page): string => "?page={$page}");
$items = $repository->findBy([], ['position' => 'ASC'], $pagination->getPerPage(), $pagination->getOffset());

$view = $listViewBuilder->build($mapper, $items, $pagination); // austral.list.view_builder
{{ include('@AustralList/list/austral_list.html.twig', {list: view}) }}

Count the items first: a requested page out of range is brought back to the first or the last page, and the offset is computed from the corrected page.

ListViewBuilder::values($mapper, $item) returns the displayed values of the visible columns of an item, by column name, to export a list.

Columns

Every column reads its value with the property accessor (property_path, the name of the column by default; an unreadable property gives null), or with a getter callable. A column holds no value: the same instance is used for every row.

Option Default
label fields.{name}.entitled Translation key of the header, false for none
label_parameters []
property_path null Path of the value, the name of the column when null
getter null callable(item): mixed
format null callable(value, item): mixed, called after the getter
translate_value false The value is a translation key
size, class null Classes of the cell (col--6...)
align null left, center or right, rendered as col--align-{align}
attr [] Attributes of the cell, or Closure(item): array
header_class, header_attr null, [] Class and attributes of the header
template, template_vars null, [] Template rendering the cell, with item, value, cell, row and list
visible true A hidden column is neither rendered nor exported
Column Widget Specific options
ValueColumn value — (an enum is displayed by its value)
DateColumn date date_format (Y-m-d)
ImageColumn image alt (string or Closure(item)), width, height, default_template; the value is the URL
TemplateColumn template template (required)
ChoicesColumn choices choices (required: value => label or value => [label, picto, class, style]), url (callable(item, value)), granted (bool or Closure(item))
SwitchColumn switch on_value (1), off_value (0), url (callable(item, value)), granted, show_label, on_label / off_label (switch.value.{value}), title, summary, pictos, button_colors, background_colors (on / off)

A column of your own extends Column, sets $widget and overrides buildVars(); the theme renders it with the block austral_list_cell_{widget}, or as a value when the block does not exist.

Actions

addAction() adds a global action (the item is null), addRowAction() an action of every row. Actions are sorted by their integer position, then by order of addition; an action with the same name replaces the previous one.

Option Default
label actions.{name} Translation key
label_parameters, translation_domain [], null null: the domain of the list
url null string or Closure(?item): ?string
picto, class null
attr [] array or Closure(?item): array
granted true bool or Closure(?item): bool; a refused action is not rendered
data_url false Renders a <button data-url="..."> instead of a link
keyboard_shortcut null data-keyboard-shortcut

ActionGroup holds several actions behind one button; it is hidden when none of its actions is granted and it has no URL.

List options

Option Default
layout table table or list
translation_domain null Domain of the labels
attr [] Attributes of the list element
empty_message list.empty Translation key, false for nothing
row_key null callable(item): string|int, the property id by default, the position otherwise
row_url null callable(item): ?string: the row gets data-url and the class has-link
row_attr, row_class null callable(item)
children null callable(item): iterable, rendered as rows of the next depth
max_depth 10 Depth of the children rendered; a cycle of children is stopped

The name of the list prefixes the DOM ids: {list}-row-{key} for a row, {list}-{column}-{key} for a cell.

View contract

list: name, layout, translation_domain, attr, empty_message, has_row_actions,
      headers[]:  name, widget, label, label_parameters, size, align, class, attr
      rows[]:     key, id, item, depth, class, attr, url, actions[], children[] (rows),
                  cells[]: name, widget, id, value, translate_value, class, size, align, attr,
                           template, template_vars, vars (widget variables)
      actions[]:  name, widget (action|group), label, label_parameters, translation_domain,
                  url, tag (a|button), picto, class, attr, actions[]
      pagination: null or page, per_page, total, nb_pages, first_url, previous_url, next_url, last_url,
                  pages[]: number, url, current

RowBuildEvent (ListEvents::ROW_BUILD) gives the view of each row before it is added: a listener can change its class, attributes, cells or actions. The children of a row are built, and dispatched, before the row.

Theme

The minimal theme @AustralList/list/austral_list.html.twig renders the contract with escaped attributes. To change the markup, extend it and override the blocks you need (see blocks.html.twig), then include your theme instead:

{# templates/list/theme.html.twig #}
{% extends '@AustralList/list/austral_list.html.twig' %}

{% block austral_list_cell_switch %}
  {{ include('components/switch.html.twig', {checked: cell.vars.checked, urls: {on: cell.vars.on_url, off: cell.vars.off_url}}) }}
{% endblock %}

Translation keys used by the theme: the labels, list.empty, list.pagination.title, list.pagination.previous, list.pagination.next, switch.value.{value}.

Documentation

The full documentation is available at https://austral.dev.

Versions

Bundle Symfony PHP Branch
4.x 7.4 ≥ 8.4 4.x
3.1 5.4 / 6.4 ≥ 8.0 3.1 (maintenance)

See the CHANGELOG and, to upgrade from 3.1, UPGRADE-4.0.

Contributing

Contributions are welcome: see the contributing guide. To report a security issue, follow the security policy.

License

MIT, see LICENSE.

Credits

Created by Matthieu Beurel. Sponsored by Yipikai Studio.