Commit 89050f92 authored by Guido Schmechel's avatar Guido Schmechel Committed by Oliver Bartsch
Browse files

[FEATURE] Introduce PSR-14 ModifyQueryForLiveSearchEvent

In order to be able to influence the query of the
LiveSearch, a new PSR-14 event ModifyQueryForLiveSearchEvent
is added. This event can be used to e.g. adjust the
query limit or to change the result order.

Resolves: #93494
Releases: main
Change-Id: I5990855d14e9de72adcba1449fb3f5336c004750
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/68528

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
parent 0c596db6
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Search\Event;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
/**
* PSR-14 event to modify the query builder instance for the live search
*/
final class ModifyQueryForLiveSearchEvent
{
private QueryBuilder $queryBuilder;
private string $table;
public function __construct(QueryBuilder $queryBuilder, string $table)
{
$this->queryBuilder = $queryBuilder;
$this->table = $table;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->queryBuilder;
}
public function getTableName(): string
{
return $this->table;
}
}
......@@ -15,7 +15,9 @@
namespace TYPO3\CMS\Backend\Search\LiveSearch;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Search\Event\ModifyQueryForLiveSearchEvent;
use TYPO3\CMS\Backend\Tree\View\PageTreeView;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
......@@ -40,43 +42,29 @@ use TYPO3\CMS\Core\Utility\MathUtility;
*/
class LiveSearch
{
/**
* @var int
*/
const RECURSIVE_PAGE_LEVEL = 99;
/**
* @var string
*/
private $queryString = '';
/**
* @var int
*/
private $startCount = 0;
/**
* @var int
*/
private $limitCount = 5;
/**
* @var string
*/
protected $userPermissions = '';
/**
* @var QueryParser
*/
protected $queryParser;
/**
* Initialize access settings
*/
public function __construct()
{
private const RECURSIVE_PAGE_LEVEL = 99;
private string $queryString = '';
private int $startCount = 0;
private int $limitCount = 5;
protected string $userPermissions = '';
protected UriBuilder $uriBuilder;
protected IconFactory $iconFactory;
protected QueryParser $queryParser;
protected EventDispatcherInterface $eventDispatcher;
public function __construct(
UriBuilder $uriBuilder,
IconFactory $iconFactory,
QueryParser $queryParser,
EventDispatcherInterface $eventDispatcher
) {
$this->uriBuilder = $uriBuilder;
$this->iconFactory = $iconFactory;
$this->queryParser = $queryParser;
$this->eventDispatcher = $eventDispatcher;
$this->userPermissions = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
$this->queryParser = GeneralUtility::makeInstance(QueryParser::class);
}
/**
......@@ -201,7 +189,8 @@ class LiveSearch
$queryBuilder->addOrderBy('uid', 'DESC');
$getRecordArray = $this->getRecordArray($queryBuilder, $tableName);
$event = $this->eventDispatcher->dispatch(new ModifyQueryForLiveSearchEvent($queryBuilder, $tableName));
$getRecordArray = $this->getRecordArray($event->getQueryBuilder(), $tableName);
}
return $getRecordArray;
......@@ -220,7 +209,6 @@ class LiveSearch
{
$collect = [];
$result = $queryBuilder->execute();
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
while ($row = $result->fetchAssociative()) {
BackendUtility::workspaceOL($tableName, $row);
if (!is_array($row)) {
......@@ -232,7 +220,7 @@ class LiveSearch
'id' => $tableName . ':' . $row['uid'],
'pageId' => $tableName === 'pages' ? $row['uid'] : $row['pid'],
'typeLabel' => $this->getTitleOfCurrentRecordType($tableName),
'iconHTML' => '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForRecord($tableName, $row, Icon::SIZE_SMALL)->render() . '</span>',
'iconHTML' => '<span title="' . htmlspecialchars($title) . '">' . $this->iconFactory->getIconForRecord($tableName, $row, Icon::SIZE_SMALL)->render() . '</span>',
'title' => BackendUtility::getRecordTitle($tableName, $row),
'editLink' => $this->getEditLink($tableName, $row),
];
......@@ -271,9 +259,8 @@ class LiveSearch
)
)
) {
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$returnUrl = (string)$uriBuilder->buildUriFromRoute('web_list', ['id' => $row['pid']]);
$editLink = (string)$uriBuilder->buildUriFromRoute('record_edit', [
$returnUrl = (string)$this->uriBuilder->buildUriFromRoute('web_list', ['id' => $row['pid']]);
$editLink = (string)$this->uriBuilder->buildUriFromRoute('record_edit', [
'edit[' . $tableName . '][' . $row['uid'] . ']' => 'edit',
'returnUrl' => $returnUrl,
]);
......
......@@ -63,6 +63,9 @@ services:
TYPO3\CMS\Backend\View\AuthenticationStyleInformation:
public: true
TYPO3\CMS\Backend\Search\LiveSearch\LiveSearch:
public: true
TYPO3\CMS\Backend\Controller\BackendController:
tags: ['backend.controller']
......
.. include:: ../../Includes.txt
==========================================================
Feature: #93494 - New PSR-14 ModifyQueryForLiveSearchEvent
==========================================================
See :issue:`93494`
Description
===========
A new PSR-14 event :php:`\TYPO3\CMS\Backend\Search\Event\ModifyQueryForLiveSearchEvent`
has been added to TYPO3 Core. This event is fired in the
:php:`\TYPO3\CMS\Backend\Search\LiveSearch\LiveSearch` class
and allows extensions to modify the :php:`QueryBuilder` instance
before execution.
The event features the following methods:
- :php:`getQueryBuilder()`: Returns the current :php:`QueryBuilder` instance
- :php:`getTableName()`: Returns the table, for which the query will be executed
Registration of the Event in your extensions' :file:`Services.yaml`:
.. code-block:: yaml
MyVendor\MyPackage\EventListener\ModifyQueryForLiveSearchEventListener:
tags:
- name: event.listener
identifier: 'my-package/modify-query-for-live-search-event-listener'
The corresponding event listener class:
.. code-block:: php
use TYPO3\CMS\Backend\Search\Event\ModifyQueryForLiveSearchEvent;
final class ModifyQueryForLiveSearchEventListener
{
public function __invoke(ModifyQueryForLiveSearchEvent $event): void
{
// Get the current instance
$queryBuilder = $event->getQueryBuilder();
// Change limit depending on the table
if ($event->getTableName() === 'pages') {
$queryBuilder->setMaxResults(2);
}
// Reset the orderBy part
$queryBuilder->resetQueryPart('orderBy');
}
}
Impact
======
It is now possible to use a new PSR-14 event for modifying the live
search query. This can e.g. be used to adjust the limit for a specific
table or to change the result order.
.. index:: Backend, PHP-API, ext:backend
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment