ModifyDefaultConstraintsForDatabaseQueryEvent
New in version 13.0
This event serves as a replacement for the removed hook
$GLOBALS.
The API class
\TYPO3\ has a
method
get which accumulates common
restrictions for a database query. The purpose is to limit queries for
TCA-based tables, filtering out disabled or scheduled records.
The PSR-14 event
\TYPO3\
allows to remove, alter or add constraints compiled by TYPO3 for a specific
table to further limit these constraints.
The event contains a list of
Composite objects, allowing
to modify them via the
get and
set methods.
Example: Restrict queries using a custom enablecolumns entry
The following example adds an additional restriction based on a custom TCA
enablecolumns entry. Assume a table stores a "purchased amount" range
per record (purchased_ / purchased_), and
only records whose range contains the current customer's purchased amount
should be selected — for every query TYPO3 builds for that table, without
having to repeat the condition manually.
Declare the custom enablecolumns entry in the table's TCA:
<?php
return [
'ctrl' => [
//...
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
'endtime' => 'endtime',
'purchasedamount' => 'purchased_amount_from,purchased_amount_to',
],
],
//...
];
Add a listener that turns this into a query constraint whenever the event fires for that table:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Domain\Event\ModifyDefaultConstraintsForDatabaseQueryEvent;
#[AsEventListener(
identifier: 'my-extension/purchased-amount-restriction',
)]
final readonly class PurchasedAmountRestrictionListener
{
public function __invoke(ModifyDefaultConstraintsForDatabaseQueryEvent $event): void
{
$table = $event->getTable();
$enableColumns = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['purchasedamount'] ?? null;
if ($enableColumns === null) {
return;
}
[$fromField, $toField] = explode(',', $enableColumns);
// strictly typed as float, so it is safe to use directly below
// without a bound query parameter
$purchasedAmount = $this->getCurrentCustomerPurchasedAmount();
$expressionBuilder = $event->getExpressionBuilder();
$event->setConstraints(array_merge(
$event->getConstraints(),
[
'purchasedamount' => $expressionBuilder->and(
$expressionBuilder->lte($table . '.' . $fromField, $purchasedAmount),
$expressionBuilder->gte($table . '.' . $toField, $purchasedAmount),
),
],
));
}
private function getCurrentCustomerPurchasedAmount(): float
{
// Your own business logic to determine the value to filter by,
// for example reading it from the current frontend user session.
return 0.0;
}
}
API
- class ModifyDefaultConstraintsForDatabaseQueryEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Core\ Domain\ Event\ Modify Default Constraints For Database Query Event
Event which is fired when compiling the list of constraints such as "deleted" and "starttime", "endtime" etc.
This Event allows for additional enableColumns to be added or removed to the list of constraints.
An example: The extension ingmar_accessctrl enables assigning more than one usergroup to content and page records