|
| 1 | +============= |
| 2 | +Custom filter |
| 3 | +============= |
| 4 | + |
| 5 | +There is possibility to add custom filters to filter managers via tagged filter service. |
| 6 | +You must create filter class, define it as a service with ``ongr_filter_manager.filter`` tag. |
| 7 | + |
| 8 | +1. Create filter class |
| 9 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | + |
| 11 | +Class must implement ``FilterInterface``. |
| 12 | + |
| 13 | +.. code-block:: php |
| 14 | +
|
| 15 | + // File location: ONGR\FilterManagerBundle\Filters\FilterInterface.pnp |
| 16 | +
|
| 17 | + /** |
| 18 | + * Resolves filter state by given request. |
| 19 | + * |
| 20 | + * @param Request $request |
| 21 | + * |
| 22 | + * @return FilterState |
| 23 | + */ |
| 24 | + public function getState(Request $request); |
| 25 | +
|
| 26 | + /** |
| 27 | + * Modifies search request by given state. Usually should be used to add query or post_filter parameters. |
| 28 | + * |
| 29 | + * @param Search $search Search request. |
| 30 | + * @param FilterState $state Current filter state. |
| 31 | + * @param SearchRequest $request State of all filters. |
| 32 | + */ |
| 33 | + public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null); |
| 34 | +
|
| 35 | + /** |
| 36 | + * Modifies search request by given state and related search. Usually is used to add aggregations into query. |
| 37 | + * |
| 38 | + * Related search does not include conditions from not related filters. Conditions made by filter |
| 39 | + * itself are also excluded on $relatedSearch. This method normally is called after modifySearch just before search |
| 40 | + * query execution |
| 41 | + * |
| 42 | + * @param Search $search |
| 43 | + * @param Search $relatedSearch |
| 44 | + * @param FilterState $state |
| 45 | + * |
| 46 | + * @return mixed |
| 47 | + */ |
| 48 | + public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null); |
| 49 | +
|
| 50 | + /** |
| 51 | + * Prepares all needed filter data to pass into view. |
| 52 | + * |
| 53 | + * @param DocumentIterator $result Search results. |
| 54 | + * @param ViewData $data Initial view data. |
| 55 | + * |
| 56 | + * @return ViewData |
| 57 | + */ |
| 58 | + public function getViewData(DocumentIterator $result, ViewData $data); |
| 59 | +
|
| 60 | +
|
| 61 | +2. Defining service |
| 62 | +~~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +Filter service must be tagged with ``ongr_filter_manager.filter`` tag, ``manager`` and ``filter_name`` nodes are required. |
| 65 | +Filter will be added to specified ``manager`` ``FilterContainer``. |
| 66 | + |
| 67 | +.. code-block:: yaml |
| 68 | +
|
| 69 | + parameters: |
| 70 | + ongr_filter_manager.filter.foo_range.class: ONGR\FilterManagerBundle\Tests\app\fixture\Acme\TestBundle\Filters\FooRange\FooRange |
| 71 | +
|
| 72 | + services: |
| 73 | + ongr_filter_manager.filter.foo_range: |
| 74 | + class: %ongr_filter_manager.filter.foo_range.class% |
| 75 | + arguments: |
| 76 | + - 'price' |
| 77 | + - 'price' |
| 78 | + tags: |
| 79 | + - { name: ongr_filter_manager.filter, manager: foo_filters, filter_name: foo_range } |
| 80 | +
|
| 81 | +Arguments from service definition can be passed to filters constructor. |
| 82 | + |
| 83 | +.. code-block:: php |
| 84 | +
|
| 85 | + // File location: ONGR\FilterManagerBundle\Tests\app\fixture\Acme\TestBundle\Filters\FooRange\FooRange.php; |
| 86 | +
|
| 87 | + /** |
| 88 | + * @param string $requestField |
| 89 | + * @param string $field |
| 90 | + */ |
| 91 | + public function __construct($requestField, $field) |
| 92 | + { |
| 93 | + $this->setRequestField($requestField); |
| 94 | + $this->setField($field); |
| 95 | + } |
| 96 | +
|
| 97 | +Filter example can be found `here <https://github.com/ongr-io/FilterManagerBundle/blob/master/Tests/app/fixture/Acme/TestBundle/Filters/FooRange/FooRange.php>`_. |
| 98 | + |
| 99 | +Services `configuration <https://github.com/ongr-io/FilterManagerBundle/blob/master/Tests/app/fixture/Acme/TestBundle/Resources/config/services.yml>`_. |
| 100 | + |
| 101 | + |
| 102 | +3. Using filter |
| 103 | +~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +Filter can be used as other filters trough ``FilterManager``, see FilterManager bundle usage `documentation <usage.html>`_. |
0 commit comments