Custom filters
It is super easy to create custom filters which will match your specific requirements.
Custom filters have to implement interface \Source and contain one public method filter.
Method filter accepts 4 arguments:
- $property (
string) - Name of the property to filter by. - $values (
mixed) - Values passed in request. - $query (
TYPO3\) - Instance of Extbase's query.CMS\ Extbase\ Persistence\ Query Interface - $apiFilter (
Source) - Instance of T3api's filter.Broker\ T3api\ Domain\ Model\ Api Filter
Important
Method filter has to return null or instance of TYPO3\.
If it returns null it won't affect the final result at all. If it returns Constraint returned constraint will be added to final query.
Note
Keep in mind that second argument ($values) is mixed type. Depending on requested URL it can be array or string.
If requests's query string contains property=hello then $values will be a string hello.
If requests's query string contains property then $values will be a array with one string inside (hello).
Consider casting $values to array inside your filter to avoid bugs ($values = ).
Example implementation of custom filter may looks as follows:
declare(strict_types=1);
namespace Vendor\Extension\Filter;
use SourceBroker\T3api\Domain\Model\ApiFilter;
use SourceBroker\T3api\Filter\FilterInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
class MyCustomFilter implements FilterInterface
{
public function filterProperty(string $property, $values, QueryInterface $query, ApiFilter $apiFilter): ?ConstraintInterface
{
return $query->equals($property, $values);
}
}
It may be useful, but not required, to extend class \Source which will give you bunch of methods inside your filter. An example of such method may be add which may be really useful to handle more complex filters, especially when you need to implement something which is out of reach Extbase's query builder. Check code inside \Source and \Source for example usages.
declare(strict_types=1);
namespace Vendor\Extension\Filter;
use SourceBroker\T3api\Domain\Model\ApiFilter;
use SourceBroker\T3api\Filter\AbstractFilter;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
class MyCustomFilter extends AbstractFilter
{
public function filterProperty(string $property, $values, QueryInterface $query, ApiFilter $apiFilter): ?ConstraintInterface
{
// ...
}
}
Note
Instance of your custom filter is created with General, so constructor injection works if the filter is a public service (public: true in Configuration/). Filters are shared instances - do not keep request specific state in them, or reset it after use.
Query modifiers
A constraint returned from filter is sometimes not enough — for example when the filter needs to apply a custom ORDER BY which has to take the complete query into account. For such cases a custom filter can additionally implement \Source:
interface QueryModifierInterface
{
public function modifyQuery(QueryInterface $query, ApiFilter $apiFilter, CollectionOperation $operation): void;
}
Method modify accepts 3 arguments:
- $query (
TYPO3\) - Instance of Extbase's query, already carrying the combined constraints of all filters.CMS\ Extbase\ Persistence\ Query Interface - $apiFilter (
Source) - The filter declaration being applied.Broker\ T3api\ Domain\ Model\ Api Filter - $operation (
Source) - The collection operation being served, so the filter can act per endpoint (path, resource, attributes).Broker\ T3api\ Domain\ Model\ Collection Operation
After the constraints of all filters have been combined and applied to the query, t3api calls modify on every filter implementing this interface. Query modifiers only ever run for collection GET operations — the same scope in which filters apply at all.
The filters run in the same order as filter — the order of their parameters in the query string as normalized by Symfony's Request::, which sorts the parameters by name with PHP's ksort — in byte order: digits first, then uppercase letters, _ and lowercase letters (Zeta comes before alpha). The position of a parameter in the URL does not matter: ?search=a&title and ?title are processed identically (search first). Only the order of nested keys is kept as requested, e.g. ?order.
Prefer Extbase's query API
Whenever Extbase's query API can express the change, use it — the query then keeps going through Extbase's regular path (DISTINCT for to-many relations, the count query, Extbase's persistence events). A typical case for a modifier instead of a plain filter constraint is a change which has to take the constraints of all other filters into account, e.g. always including featured records, whatever the other filters match:
public function modifyQuery(QueryInterface $query, ApiFilter $apiFilter, CollectionOperation $operation): void
{
$featured = $query->equals('featured', true);
$constraint = $query->getConstraint();
$query->matching($constraint === null ? $featured : $query->logicalOr($constraint, $featured));
}
Registration
To use your new custom filter is it just needed to pass it as first parameter into @Api annotation:
use SourceBroker\T3api\Annotation as T3api;
use Vendor\Extension\Filter\MyCustomFilter;
/**
* @T3api\ApiFilter(
* MyCustomFilter::class,
* properties={"username"},
* )
*/
class User extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{
}