Feature: #107889 - Introduce TCA option "itemsProcessors"
See forge#107889
Description
As part of centralizing and improving the processing of items for select,
check, and radio type fields, a new TCA option
items has been introduced as a replacement for
items. This option is an array, allowing any number of
processors to be called instead of just one. Processors are ordered by their
array key (numerical) and executed in that order. Processors can receive
arbitrary data through the
$context->processor property.
All processors must implement the
Items interface.
Processor methods receive two parameters:
a
Select instance
containing the items, and an
Items instance
providing access to table, field, row data, and configuration. The processor
must return a
Select. This means that added items can no
longer be untyped arrays, making the entire process cleaner and safer.
A new Page TSconfig option is also available, mirroring the existing one for
items. See the example below for syntax.
Example
The TCA registration might look like this:
use MyVendor\MyExtension\Processors\SpecialRelationsProcessor;
'relation' => [
'label' => 'Relational field',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'items' => [
[
'value' => 0,
'label' => '',
],
],
'foreign_table' => 'some_foreign_table',
'itemsProcessors' => [
100 => [
'class' => SpecialRelationsProcessor::class,
'parameters' => [
'foo' => 'bar',
],
],
50 => [
'class' => SpecialRelationsProcessor2::class,
],
],
],
],
In this example,
Special will be called before
Special.
Here is an example processor:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Processors;
use TYPO3\CMS\Core\DataHandling\ItemsProcessorContext;
use TYPO3\CMS\Core\DataHandling\ItemsProcessorInterface;
use TYPO3\CMS\Core\Schema\Struct\SelectItem;
use TYPO3\CMS\Core\Schema\Struct\SelectItemCollection;
final class SpecialRelationsProcessor implements ItemsProcessorInterface
{
public function processItems(
SelectItemCollection $items,
ItemsProcessorContext $context,
): SelectItemCollection {
$items->add(
new SelectItem(
type: 'select',
label: 'Extra item',
value: 42,
)
);
return $items;
}
}
The
$context->processor property contains any parameters
defined in the TCA declaration.
Using Page TSconfig to pass custom parameters to the processor would look like this:
TCEFORM.example_table.content.itemsProcessors.100.foo = bar
Note that the numerical key of the processor must be reused.
With this setup, the class
Special receives the PHP
array
['foo' => 'bar'] in the
$context->field property.
The class
Special receives an empty array
[] (since it is registered with the key
50).
Registration of processors is also possible within FlexForms:
<some_selector>
<label>Choice</label>
<config>
<type>select</type>
<renderType>selectSingle</renderType>
<itemsProcessors>
<numIndex index="100">
<class>MyVendor\MyPackage\Processors\SpecialRelationsProcessor</class>
</numIndex>
</itemsProcessors>
</config>
</some_selector>
Impact
It is still possible to use
items, but switching to
items is recommended because it offers two main advantages:
- Being an array, it allows extensions to add processors on top of existing ones and to define their execution order.
- The processing chain is strictly typed, ensuring safer and more reliable code.
If both
items and
items are defined,
both are executed, with
items executed first.
Tip
The naming itemsProcessors, using a double plural form, was chosen for two reasons. First, it complements the former plural form itemsUserFunc. Second, multiple processors can be defined that operate on the collection of all "items", not just on a single item.