Feature: #102337 - PSR-14 event for modifying record list download presets
See forge#102337
Description
A new PSR-14 event
\TYPO3\
has been introduced to manipulate the list of available download presets in
the Web > List module.
See Feature: #102337 - Presets for download of record lists for a detailed description of how to utilize presets when downloading a set of records from the backend in CSV or JSON format.
The event class offers the following methods:
get
: Returns a list of presets set via TSconfigPresets () set
: Sets a modified list of presets.Presets () get
: Returns the database table name that a preset applies to.Database Table () get
: Returns the PSR Request object for the context of the request.Request () get
: Returns the page ID of the originating page.Id ()
Note that the event is dispatched for one specific database table. If an event listener is created to attach presets to different tables, the listener method must check for the table name, as shown in the example below.
If no download presets exist for a given table, the PSR-14 event can still
be used to modify and add presets to it via the
set
method.
The array passed from
get
to
set
can contain
an array collection of
\TYPO3\
objects with the array key using the preset label.
The existing presets can be retrieved with these getters:
$preset->get
: Name of the preset (can utilize LLL translations), optional.Label () $preset->get
: Array of database table column names.Columns () $preset->get
: Identifier of the preset (manually set or calculated based on label and columns)Identifier ()
The event listener can also remove array indexes or columns of existing
array entries by passing a newly constructed
Download
object with the
changed label
and columns
constructor properties.
Example
The corresponding event listener class:
<?php
declare(strict_types=1);
namespace Vendor\MyPackage\RecordList\EventListener;
use TYPO3\CMS\Backend\RecordList\Event\BeforeRecordDownloadPresetsAreDisplayedEvent;
use TYPO3\CMS\Backend\RecordList\DownloadPreset;
use TYPO3\CMS\Core\Attribute\AsEventListener;
#[AsEventListener(identifier: 'my-package/modify-record-list-preset')]
final readonly class PresetListener
{
public function __invoke(BeforeRecordDownloadPresetsAreDisplayedEvent $event): void
{
$presets = $event->getPresets();
switch ($event->getDatabaseTable()) {
case 'be_users':
$presets[] = new DownloadPreset('PSR-14 preset', ['uid','email']);
break;
case 'pages':
$presets[] = new DownloadPreset('PSR-14 preset', ['title']);
$presets[] = new DownloadPreset('Another PSR-14 preset', ['title', 'doktype']);
break;
case 'tx_myvendor_myextension':
$presets[] = new DownloadPreset('PSR-14 preset', ['uid', 'something']);
break;
}
$presets[] = new DownloadPreset('Available everywhere, simple UID list', ['uid']);
$presets['some-identifier'] = new DownloadPreset('Overwrite preset', ['uid, pid'], 'some-identifier');
$event->setPresets($presets);
}
}
Impact
Using the PSR-14 event
Before
it is now possible to modify the presets of each table for
downloading / exporting a list of such records via the Web > List
module.