Skip to content

Commit 3f612ef

Browse files
linawolffroemken
andauthored
[FEATURE] Allow extending page tree filter functionality (#5195)
* [FEATURE] Allow extending page tree filter functionality Resolves TYPO3-Documentation/Changelog-To-Doc#1184 Releases: main * [FEATURE] Allow extending page tree filter functionality Resolves TYPO3-Documentation/Changelog-To-Doc#1184 Releases: main * Apply suggestions from code review Co-authored-by: Stefan Frömken <[email protected]> --------- Co-authored-by: Stefan Frömken <[email protected]>
1 parent fa25431 commit 3f612ef

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

Documentation/ApiOverview/Backend/PageTree.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ PSR-14 events to influence the functionality of the page tree
3232
:ref:`AfterRawPageRowPreparedEvent`
3333
Allows to modify the populated properties of a page and children records
3434
before the page is displayed in a page tree.
35+
:ref:`BeforePageTreeIsFilteredEvent`
36+
Allows developers to extend the page trees filter's functionality and
37+
process the given search phrase in more advanced ways.
3538

3639
.. _page-tree-tsconfig:
3740

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. include:: /Includes.rst.txt
2+
.. index:: Events; BeforePageTreeIsFilteredEvent
3+
.. _BeforePageTreeIsFilteredEvent:
4+
5+
=============================
6+
BeforePageTreeIsFilteredEvent
7+
=============================
8+
9+
.. versionadded:: 14.0
10+
This PSR-14 event was introduced to add custom functionality and advanced
11+
evaluations to the the page tree filter.
12+
13+
The PSR-14 :php:`\TYPO3\CMS\Backend\Tree\Repository\BeforePageTreeIsFilteredEvent`
14+
allows developers to extend the page trees filter's functionality and process the
15+
given search phrase in more advanced ways.
16+
17+
The page tree is one of the central components in the TYPO3 backend,
18+
particularly for editors. However, in large installations, the page tree can
19+
quickly become overwhelming and difficult to navigate. To maintain a clear
20+
overview, the page tree can be filtered using basic terms, such as the page
21+
title or ID.
22+
23+
.. _BeforePageTreeIsFilteredEvent-example:
24+
25+
Example: Add evaluation of document types to the page tree search filter
26+
========================================================================
27+
28+
The event listener class, using the PHP attribute :php:`#[AsEventListener]` for
29+
registration, adds additional conditions to the filter.
30+
31+
.. literalinclude:: _BeforePageTreeIsFilteredEvent/_MyEventListener.php
32+
:caption: EXT:my_extension/Classes/Backend/EventListener/MyEventListener.php
33+
34+
.. _BeforePageTreeIsFilteredEvent-api:
35+
36+
BeforePageTreeIsFilteredEvent API
37+
=================================
38+
39+
The event provides the following member properties:
40+
41+
`$searchParts`:
42+
The search parts to be used for filtering
43+
`$searchUids`:
44+
The uids to be used for filtering by a special search part, which
45+
is added by Core always after listener evaluation
46+
`$searchPhrase`
47+
The complete search phrase, as entered by the user
48+
`$queryBuilder`:
49+
The current :php-short:`\TYPO3\CMS\Core\Database\Query\QueryBuilder`
50+
51+
.. important::
52+
53+
The :php-short:`\TYPO3\CMS\Core\Database\Query\QueryBuilder` instance is provided solely
54+
for context and to simplify the creation of
55+
search parts by using the :php-short:`\TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder`
56+
via :php:`QueryBuilder->expr()`. The instance itself **must not** be modified by listeners and
57+
is not considered part of the public API. TYPO3 reserves the right to change the instance at
58+
any time without prior notice.
59+
60+
.. include:: /CodeSnippets/Events/Backend/BeforePageTreeIsFilteredEvent.rst.txt
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Frontend\EventListener;
6+
7+
use TYPO3\CMS\Backend\Tree\Repository\BeforePageTreeIsFilteredEvent;
8+
use TYPO3\CMS\Core\Attribute\AsEventListener;
9+
use TYPO3\CMS\Core\Database\Connection;
10+
11+
final class MyEventListener
12+
{
13+
#[AsEventListener]
14+
public function removeFetchedPageContent(BeforePageTreeIsFilteredEvent $event): void
15+
{
16+
// Adds another uid to the filter
17+
$event->searchUids[] = 123;
18+
19+
// Adds evaluation of doktypes to the filter
20+
if (preg_match('/doktype:([0-9]+)/i', $event->searchPhrase, $match)) {
21+
$doktype = $match[1];
22+
$event->searchParts = $event->searchParts->with(
23+
$event->queryBuilder->expr()->eq(
24+
'doktype',
25+
$event->queryBuilder->createNamedParameter($doktype, Connection::PARAM_INT),
26+
),
27+
);
28+
}
29+
}
30+
}

Documentation/CodeSnippets/Config/Api/Events/EventsBackend.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,10 @@
279279
'targetFileName' => 'CodeSnippets/Events/Backend/PasswordHasBeenResetEvent.rst.txt',
280280
'withCode' => false,
281281
],
282+
[
283+
'action' => 'createPhpClassDocs',
284+
'class' => \TYPO3\CMS\Backend\Tree\Repository\BeforePageTreeIsFilteredEvent::class,
285+
'targetFileName' => 'CodeSnippets/Events/Backend/BeforePageTreeIsFilteredEvent.rst.txt',
286+
'withCode' => false,
287+
],
282288
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
2+
.. php:namespace:: TYPO3\CMS\Backend\Tree\Repository
3+
4+
.. php:class:: BeforePageTreeIsFilteredEvent
5+
6+
Listeners to this event will be able to modify the search parts, to be used to filter the page tree
7+
8+
.. php:attr:: searchParts
9+
:public:
10+
11+
.. php:attr:: searchUids
12+
:public:
13+
14+
.. php:attr:: searchPhrase
15+
:readonly:
16+
:public:
17+
18+
.. php:attr:: queryBuilder
19+
:readonly:
20+
:public:

0 commit comments

Comments
 (0)