ModifyResultItemInLiveSearchEvent
The PSR-14 event
\TYPO3\
allows extension developers to take control over search result
items rendered in the backend search.
Example: Add a history action to content element results
The following listener adds an action to every content element in the search results. The action opens the history of the record:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Backend\EventListener;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Search\Event\ModifyResultItemInLiveSearchEvent;
use TYPO3\CMS\Backend\Search\LiveSearch\DatabaseRecordProvider;
use TYPO3\CMS\Backend\Search\LiveSearch\ResultItemAction;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Localization\TranslatorInterface;
#[AsEventListener(
identifier: 'my-extension/live-search-show-history',
)]
final readonly class ShowHistoryActionListener
{
private TranslatorInterface $translator;
public function __construct(
private IconFactory $iconFactory,
LanguageServiceFactory $languageServiceFactory,
private UriBuilder $uriBuilder,
) {
$this->translator = $languageServiceFactory->createFromUserPreferences($GLOBALS['BE_USER']);
}
public function __invoke(ModifyResultItemInLiveSearchEvent $event): void
{
$resultItem = $event->getResultItem();
if ($resultItem->getProviderClassName() !== DatabaseRecordProvider::class) {
return;
}
if (($resultItem->getExtraData()['table'] ?? null) !== 'tt_content') {
return;
}
/**
* WARNING: THIS EXAMPLE OMITS ANY ACCESS CHECK FOR SIMPLICITY REASONS - DO NOT USE AS-IS
*/
$showHistoryAction = (new ResultItemAction('view_history'))
->setLabel($this->translator->label('core.mod-web-list:history'))
->setIcon($this->iconFactory->getIcon('actions-document-history-open', IconSize::SMALL))
->setUrl((string)$this->uriBuilder->buildUriFromRoute('record_history', [
'element' => $resultItem->getExtraData()['table'] . ':' . $resultItem->getExtraData()['uid'],
]));
$resultItem->addAction($showHistoryAction);
}
}
Example: Show a property in the detail panel of a search result
New in version 14.3 Important: #109787 - Add files to livesearch results
A search result can show a thumbnail and a list of properties in its detail
panel. Call
Result
with a label and a value to add a property. Call
Result
with the URL of a preview image to show a thumbnail instead of the icon.
The following listener shows the UID of every content element in the detail panel:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Backend\EventListener;
use TYPO3\CMS\Backend\Search\Event\ModifyResultItemInLiveSearchEvent;
use TYPO3\CMS\Backend\Search\LiveSearch\DatabaseRecordProvider;
use TYPO3\CMS\Core\Attribute\AsEventListener;
#[AsEventListener(
identifier: 'my-extension/live-search-record-uid',
)]
final readonly class RecordUidPropertyListener
{
public function __invoke(ModifyResultItemInLiveSearchEvent $event): void
{
$resultItem = $event->getResultItem();
$provider = $resultItem->getProviderClassName();
if ($provider !== DatabaseRecordProvider::class) {
return;
}
$extraData = $resultItem->getExtraData();
if (($extraData['table'] ?? null) !== 'tt_content') {
return;
}
// Shown in the detail panel of the result
$resultItem->addProperty('Record UID', (string)$extraData['uid']);
}
}