ModifyDefaultConstraintsForDatabaseQueryEvent 

New in version 13.0

This event serves as a replacement for the removed hook $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['addEnableColumns'] .

The API class \TYPO3\CMS\Core\Domain\Repository\PageRepository has a method getDefaultConstraints() 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\CMS\Core\Domain\Event\ModifyDefaultConstraintsForDatabaseQueryEvent 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 CompositeExpression objects, allowing to modify them via the getConstraints() and setConstraints(array $constraints) 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_amount_from / purchased_amount_to), 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:

EXT:my_extension/Configuration/TCA/tx_myextension_domain_model_product.php
<?php

return [
    'ctrl' => [
        //...
        'enablecolumns' => [
            'disabled' => 'hidden',
            'starttime' => 'starttime',
            'endtime' => 'endtime',
            'purchasedamount' => 'purchased_amount_from,purchased_amount_to',
        ],
    ],
    //...
];
Copied!

Add a listener that turns this into a query constraint whenever the event fires for that table:

EXT:my_extension/Classes/EventListener/PurchasedAmountRestrictionListener.php
<?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;
    }
}
Copied!

API 

class ModifyDefaultConstraintsForDatabaseQueryEvent
Fully qualified name
\TYPO3\CMS\Core\Domain\Event\ModifyDefaultConstraintsForDatabaseQueryEvent

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

getTable ( )
Returns
string
getTableAlias ( )
Returns
string
getExpressionBuilder ( )
Returns
TYPO3CMSCoreDatabaseQueryExpressionExpressionBuilder
getConstraints ( )
Returns
array<string,CompositeExpression|string>
setConstraints ( array $constraints)
param $constraints

the constraints

getEnableFieldsToIgnore ( )
Returns
array
getContext ( )
Returns
TYPO3CMSCoreContextContext