ModifyConstraintsForLiveSearchEvent
New in version 14.2
The PSR-14 event
Modify
is fired in the
\TYPO3\ class
and allows search constraints to be added or removed before a search is executed.
This event allows extensions to modify the
Composite OR-combined constraints
by adding or removing constraints from an array.
This event was necessary because the main
query modifĂcation event
\TYPO3\
cannot access query constraints.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyPackage\Backend\Search\EventListener;
use TYPO3\CMS\Backend\Search\Event\ModifyConstraintsForLiveSearchEvent;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Database\ConnectionPool;
final readonly class MyEventListener
{
public function __construct(private ConnectionPool $connectionPool) {}
#[AsEventListener('my-package/livesearch-enhanced')]
public function __invoke(ModifyConstraintsForLiveSearchEvent $event): void
{
if ($event->getTableName() !== 'pages') {
return;
}
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
// Add a constraint so that pages marked with "show_in_all_results=1"
// will always be shown.
$constraints[] = $queryBuilder->expr()->eq(
'show_in_all_results',
1,
);
$event->addConstraints(...$constraints);
}
}
API
- class ModifyConstraintsForLiveSearchEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Backend\ Search\ Event\ Modify Constraints For Live Search Event
PSR-14 event to modify the query builder instance for the live search
- addConstraints ( TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression|string ...$constraints)
-
Note that we only add a single/multiple constraints and do not allow to remove or override existing ones. This is a safeguard to not overwrite security-related query constraints.
- param $constraints
-
the constraints
Hint
Constraints are only intended to be added to this event so that there is no negative impact on security-related mandatory constraints added by Core/extensions. This means that constraints cannot be removed after they have been added.