https://github.com/remindgmbh/extbase-searchfilter
Create complex filters with typo3 records.
https://github.com/remindgmbh/extbase-searchfilter
extbase filter search typo3
Last synced: 10 months ago
JSON representation
Create complex filters with typo3 records.
- Host: GitHub
- URL: https://github.com/remindgmbh/extbase-searchfilter
- Owner: remindgmbh
- License: gpl-3.0
- Created: 2021-06-25T15:10:23.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-07T11:53:08.000Z (over 1 year ago)
- Last Synced: 2025-08-06T14:11:21.387Z (10 months ago)
- Topics: extbase, filter, search, typo3
- Language: PHP
- Homepage:
- Size: 213 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# REMIND - Searchfilter Library
An effort was made to provide a generalized approach to providing search filters.
[travis-img]: https://img.shields.io/travis/remindgmbh/extbase-searchfilter.svg?style=flat-square
[codecov-img]: https://img.shields.io/codecov/c/github/remindgmbh/extbase-searchfilter.svg?style=flat-square
[php-v-img]: https://img.shields.io/packagist/php-v/remind/extbase-searchfilter?style=flat-square
[github-issues-img]: https://img.shields.io/github/issues/remindgmbh/extbase-searchfilter.svg?style=flat-square
[contrib-welcome-img]: https://img.shields.io/badge/contributions-welcome-blue.svg?style=flat-square
[license-img]: https://img.shields.io/github/license/remindgmbh/extbase-searchfilter.svg?style=flat-square
[styleci-img]: https://styleci.io/repos/380275185/shield
[![travis-img]](https://travis-ci.com/github/remindgmbh/extbase-searchfilter)
[![codecov-img]](https://codecov.io/gh/remindgmbh/extbase-searchfilter)
[![styleci-img]](https://github.styleci.io/repos/380275185)
[![php-v-img]](https://packagist.org/packages/remind/extbase-searchfilter)
[![github-issues-img]](https://github.com/remindgmbh/extbase-searchfilter/issues)
[![contrib-welcome-img]](https://github.com/remindgmbh/extbase-searchfilter/blob/master/CONTRIBUTING.md)
[![license-img]](https://github.com/remindgmbh/extbase-searchfilter/blob/master/LICENSE)
---
## How to use
Start with a n-dimensional array filter representation.
```php
[
/* A record type with a given value */
'someRecord' => 123
],
'someOtherFilter' => [
'anotherThing' => [ 'foo', 'bar', 'baz' ]
]
];
$strategy = new MyFilterSearchFilterStrategy();
$strategy->setObjectManager($objectManager);
$strategy->setFilter($filter);
$strategy->setQuery();
/*
* This will return a TYPO3\CMS\Extbase\Persistence\QueryInterface instance
* that can be used like any other TYPO3 query object or NULL when an error
* occured.
*/
$query = $strategy->parse();
$result = $query === null ? null : $query->execute();
```
Create implementing class.
```php
searchFieldNamespace = self::STRATEGY_NS;
}
/**
* This methods implementation should apply group based processing,
* or if no special group handling is necessary, it should call
* processFieldGroup() and return the result.
*
* @return void
*/
protected function applyGroupRules(): void
{
$constraint = $this->processFieldGroup();
if ($constraint !== null) {
$this->constraints[] = $constraint;
}
}
/**
* Implement this method and create a query instance that will be used
* by this instance.
*
* #TODO The methods visibility is likely an artifact from an earlier
* implementation and is bound to change in a later version.
*
* @return void
*/
public function setQuery(): void
{
/* @var $repository SomeRepository */
$repository = $this->objectManager->get(SomeRepository::class);
$this->query = $repository->createQuery();
}
}
```
Create class to process filter values.
```php
[
* 'someRecord' => 123
* ]
* ]
*
* It will be used to get the records for the SomeRepository, from which the
* query originates, by using the uid of another Record from SomeOtherRepository
* that has some relation to the records of SomeRepository.
*
*/
class SomeRecordFieldStrategy extends AbstractFieldStrategy
{
/**
* The $this->filterValue should be set to the value from the filter
* object, which is '123' in this case.
*
* @return ConstraintInterface|null
*/
public function process(): ?ConstraintInterface
{
/* If no numeric value was given */
if (!is_numeric($this->filterValue)) {
return null; // do not process any further
}
/* @var $repository SomeOtherRepository */
$repository = $this->objectManager->get(SomeOtherRepository::class);
/* Get all records matching the '123' filter value in whatever field */
$records = $repository->findBySomeField($this->filterValue);
/* Abort processing */
if ($records->count() === 0) {
return null;
}
$constraints = [];
foreach ($records as $record) {
/* @var $record SomeOtherModel /*
/* @var $objects ObjectStorage */
$objects = $record->getRelatedObjects();
/* Abort processing */
if ($objects->count() === 0) {
return null;
}
$uids = [];
/* Get uid values from objects */
foreach ($objects as $object) {
$uids[] = $object->getUid();
}
/*
* Match query uid from SomeRepository with values returned
* from SomeOtherRepository
*/
return $this->query->in('uid', $uids);
}
/* Default return */
return null;
}
}
```