Callbacks
Callbacks allow you to hook into the configuration and lifecycle of filter elements and list types using PHP 8 attributes. This mechanism replaces the traditional Contao DCA callbacks with a more flexible, service-oriented approach.
1. Registration
To register a callback, use the #[AsFilterCallback] or #[AsListCallback] attribute on a service method.
#[AsFilterCallback]
Used for logic related to Filter Elements.
use HeimrichHannot\FlareBundle\DependencyInjection\Attribute\AsFilterCallback;
use HeimrichHannot\FlareBundle\Specification\FilterDefinition;
use HeimrichHannot\FlareBundle\Specification\ListSpecification;
use HeimrichHannot\FlareBundle\Query\ListExecutionContext;
#[AsFilterCallback(
element: 'my_filter_type',
target: 'fields.myField.options',
priority: 10
)]
public function getMyFieldOptions(
ListSpecification $list,
FilterDefinition $filter,
ListExecutionContext $context
): array {
return [
'value1' => 'Label 1',
'value2' => 'Label 2',
];
}
#[AsListCallback]
Used for logic related to List Types.
use HeimrichHannot\FlareBundle\DependencyInjection\Attribute\AsListCallback;
#[AsListCallback(
element: 'my_list_type',
target: 'config.onload'
)]
public function handleOnLoad(): void
{
// Custom logic when the list type is loaded
}
2. Filter Element Callbacks
Filterconfig.onloadExecuted when the DataContainer object for the filter element is initialized.
Available Arguments
FilterModelThe current filter element model
ListModelThe list model the filter belongs to
DataContainerThe Contao DataContainer instance
Return Type
voidExample Implementation
#[AsFilterCallback(element: 'my_filter', target: 'config.onload')]
public function onLoad(FilterModel $filter, DataContainer $dc): void
{
// Custom logic
}
Filterfields.<field>.optionsUsed to dynamically provide options for a filter element field.
Available Arguments
FilterModelThe current filter element model
ListModelThe list model the filter belongs to
DataContainerThe Contao DataContainer instance
FilterDefinitionInternal filter definition
ListSpecificationThe list specification
ListExecutionContextCurrent execution context
array $tablesAssociated tables for the list
string $targetTableThe target table for the filter
Return Type
arrayReturn an associative array of selectable options where the array key is the stored value and the array value is the backend label.
Example Implementation
#[AsFilterCallback(element: 'my_filter', target: 'fields.myField.options')]
public function getOptions(array $tables, string $targetTable): array
{
return ['val' => 'Label'];
}
Filterfields.<field>.loadTriggered when a filter element field value is loaded from the database.
Available Arguments
mixed $valueThe raw value from the database
FilterModelThe current filter element model
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
mixedReturn the transformed value that should be used in the backend form after loading.
Example Implementation
#[AsFilterCallback(element: 'my_filter', target: 'fields.myField.load')]
public function onLoad($value, FilterModel $filter): mixed
{
return $value === 'foo' ? 'bar' : $value;
}
Filterfields.<field>.saveTriggered before a filter element field value is saved to the database.
Available Arguments
mixed $valueThe value to be saved
FilterModelThe current filter element model
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
mixedReturn the normalized value that should actually be persisted to the database.
Example Implementation
#[AsFilterCallback(element: 'my_filter', target: 'fields.myField.save')]
public function onSave($value, FilterModel $filter): mixed
{
return strtoupper($value);
}
3. List Type Callbacks
Listconfig.onloadTriggered when the list configuration (DCA) is initialized.
Available Arguments
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
voidExample Implementation
#[AsListCallback(element: 'my_list', target: 'config.onload')]
public function onLoad(ListModel $list, DataContainer $dc): void
{
// Modify DCA or state
}
Listfields.<field>.optionsUsed to provide options for list configuration fields.
Available Arguments
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
arrayReturn an associative array of selectable options where the array key is the stored value and the array value is the backend label.
Example Implementation
#[AsListCallback(element: 'my_list', target: 'fields.myField.options')]
public function getOptions(ListModel $list): array
{
return ['a' => 'Option A'];
}
Listfields.<field>.loadTriggered when a list configuration field value is loaded.
Available Arguments
mixed $valueThe raw value
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
mixedReturn the transformed value that should be shown in the backend form after loading.
Example Implementation
#[AsListCallback(element: 'my_list', target: 'fields.myField.load')]
public function onLoad($value): mixed
{
return $value;
}
Listfields.<field>.saveTriggered before a list configuration field value is saved.
Available Arguments
mixed $valueThe value to be saved
ListModelThe list model
DataContainerThe Contao DataContainer instance
Return Type
mixedReturn the normalized value that should be persisted for the list configuration field.
Example Implementation
#[AsListCallback(element: 'my_list', target: 'fields.myField.save')]
public function onSave($value): mixed
{
return $value;
}
4. Argument Resolution Details
- Positional Arguments: Some targets provide a value as the first argument (marked as Positional above).
- Type-hinted Arguments: You can type-hint any of the following classes to have them injected:
HeimrichHannot\FlareBundle\Model\FilterModelHeimrichHannot\FlareBundle\Model\ListModelContao\DataContainerHeimrichHannot\FlareBundle\Specification\FilterDefinitionHeimrichHannot\FlareBundle\Specification\ListSpecificationHeimrichHannot\FlareBundle\Query\ListExecutionContext
- Named Arguments: For targets like
options, additional data is passed that can be accessed by the parameter name (e.g.,array $tables,string $targetTable).