Skip to content

Commit f298155

Browse files
author
Mantas Urnieža
committed
Merge pull request #85 from asev/tagged_filters
Tagged filters
2 parents eb80027 + a5c58da commit f298155

19 files changed

+135
-6
lines changed

DependencyInjection/Configuration.php

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ private function buildFilterTree($filterName)
129129
->scalarNode('field')
130130
->info('Document field name.')
131131
->end()
132+
->arrayNode('tags')
133+
->info('Filter tags that will be passed to view data.')
134+
->prototype('scalar')->end()
135+
->end()
132136
->end();
133137

134138
switch ($filterName) {

DependencyInjection/Filter/AbstractFilterFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ protected function configure(Definition $definition, array $configuration)
5151
$configuration['request_field'],
5252
]
5353
);
54+
$definition->addMethodCall(
55+
'setTags',
56+
[
57+
$configuration['tags'],
58+
]
59+
);
5460
}
5561

5662
/**

Filters/FilterInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@ public function preProcessSearch(Search $search, Search $relatedSearch, FilterSt
6464
* @return ViewData
6565
*/
6666
public function getViewData(DocumentIterator $result, ViewData $data);
67+
68+
/**
69+
* Returns all tags assigned to the filter.
70+
*
71+
* @return array
72+
*/
73+
public function getTags();
6774
}

Filters/ViewData.php

+31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class ViewData
2121
*/
2222
private $state;
2323

24+
/**
25+
* @var array
26+
*/
27+
private $tags;
28+
2429
/**
2530
* @var array Url parameters representing current filter state.
2631
*/
@@ -84,6 +89,32 @@ public function setState(FilterState $state)
8489
$this->state = $state;
8590
}
8691

92+
/**
93+
* @return string
94+
*/
95+
public function getTags()
96+
{
97+
return $this->tags;
98+
}
99+
100+
/**
101+
* @param string $tag
102+
*
103+
* @return bool
104+
*/
105+
public function hasTag($tag)
106+
{
107+
return in_array($tag, $this->tags, true);
108+
}
109+
110+
/**
111+
* @param string $tags
112+
*/
113+
public function setTags($tags)
114+
{
115+
$this->tags = $tags;
116+
}
117+
87118
/**
88119
* @return array
89120
*/

Filters/Widget/AbstractSingleRequestValueFilter.php

+21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ abstract class AbstractSingleRequestValueFilter implements FilterInterface
2828
*/
2929
private $requestField;
3030

31+
/**
32+
* @var array
33+
*/
34+
private $tags = [];
35+
3136
/**
3237
* {@inheritdoc}
3338
*/
@@ -61,4 +66,20 @@ public function setRequestField($requestField)
6166
{
6267
$this->requestField = $requestField;
6368
}
69+
70+
/**
71+
* @return string
72+
*/
73+
public function getTags()
74+
{
75+
return $this->tags;
76+
}
77+
78+
/**
79+
* @param string $tags
80+
*/
81+
public function setTags($tags)
82+
{
83+
$this->tags = $tags;
84+
}
6485
}

Resources/doc/filter/choice.rst

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Configuration
5454
+------------------------+--------------------------------------------------------------------------------------+
5555
| `sort` | Sorts the choices based on your configuration. |
5656
+------------------------+--------------------------------------------------------------------------------------+
57+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
58+
+------------------------+--------------------------------------------------------------------------------------+
5759

5860
Sorting configuration
5961
---------------------
@@ -111,6 +113,10 @@ View data returned by this filter to be used in template:
111113
+-------------------------+--------------------------------------------------+
112114
| getChoices() | Returns a list of available choices |
113115
+-------------------------+--------------------------------------------------+
116+
| getTags() | Lists all tags specified at filter configuration |
117+
+-------------------------+--------------------------------------------------+
118+
| hasTag($tag) | Checks if filter has the specific tag |
119+
+-------------------------+--------------------------------------------------+
114120

115121
Each choice has its own data:
116122

Resources/doc/filter/custom_filter.rst

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ Class must implement ``FilterInterface``.
5656
*/
5757
public function getViewData(DocumentIterator $result, ViewData $data);
5858
59+
/**
60+
* Returns all tags assigned to the filter.
61+
*
62+
* @return array
63+
*/
64+
public function getTags();
65+
5966
6067
2. Defining service
6168
-------------------

Resources/doc/filter/document_field.rst

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Configuration
3838
+------------------------+--------------------------------------------------------------------------------------+
3939
| `field` | Specifies the field in repository to apply this filter on. (e.g. `category_id`) |
4040
+------------------------+--------------------------------------------------------------------------------------+
41+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
42+
+------------------------+--------------------------------------------------------------------------------------+
4143

4244
Example:
4345

@@ -75,4 +77,8 @@ View data returned by this filter to be used in template:
7577
+-------------------------+--------------------------------------------------+
7678
| getUrlParameters() | Url parameters representing current filter state |
7779
+-------------------------+--------------------------------------------------+
80+
| getTags() | Lists all tags specified at filter configuration |
81+
+-------------------------+--------------------------------------------------+
82+
| hasTag($tag) | Checks if filter has the specific tag |
83+
+-------------------------+--------------------------------------------------+
7884

Resources/doc/filter/match.rst

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Configuration
4040
+------------------------+--------------------------------------------------------------------------------------+
4141
| `field` | Specifies the field in repository to apply this filter on. (e.g. `item_color`) |
4242
+------------------------+--------------------------------------------------------------------------------------+
43+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
44+
+------------------------+--------------------------------------------------------------------------------------+
4345

4446
Example:
4547

@@ -78,6 +80,10 @@ View data returned by this filter to be used in template:
7880
+-------------------------+--------------------------------------------------+
7981
| getUrlParameters() | Url parameters representing current filter state |
8082
+-------------------------+--------------------------------------------------+
83+
| getTags() | Lists all tags specified at filter configuration |
84+
+-------------------------+--------------------------------------------------+
85+
| hasTag($tag) | Checks if filter has the specific tag |
86+
+-------------------------+--------------------------------------------------+
8187

8288
* `Choice filter <choice.html>`_
8389
* `Multi choice filter <multi_choice.html>`_

Resources/doc/filter/multi_choice.rst

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Configuration
5959
+------------------------+--------------------------------------------------------------------------------------------------+
6060
| `sort` | Choices can also be sorted. You can read more about this [here](choice.md#sorting-configuration).|
6161
+------------------------+--------------------------------------------------------------------------------------------------+
62+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
63+
+------------------------+--------------------------------------------------------------------------------------------------+
6264

6365
Example:
6466

@@ -98,6 +100,10 @@ View data returned by this filter to be used in template:
98100
+-------------------------+--------------------------------------------------+
99101
| getChoices() | Returns a list of available choices |
100102
+-------------------------+--------------------------------------------------+
103+
| getTags() | Lists all tags specified at filter configuration |
104+
+-------------------------+--------------------------------------------------+
105+
| hasTag($tag) | Checks if filter has the specific tag |
106+
+-------------------------+--------------------------------------------------+
101107

102108
Each choice has its own data:
103109

Resources/doc/filter/pager.rst

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Configuration
4949
+------------------------+--------------------------------------------------------------------------------------+
5050
| `max_pages` | Maximum number of pages displayed in pager at once (default `8`). |
5151
+------------------------+--------------------------------------------------------------------------------------+
52+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
53+
+------------------------+--------------------------------------------------------------------------------------+
5254

5355
Example:
5456

@@ -89,3 +91,7 @@ View data returned by this filter to be used in template:
8991
+-------------------------+--------------------------------------------------+
9092
| getPagerService() | Returns pager service to be used in template |
9193
+-------------------------+--------------------------------------------------+
94+
| getTags() | Lists all tags specified at filter configuration |
95+
+-------------------------+--------------------------------------------------+
96+
| hasTag($tag) | Checks if filter has the specific tag |
97+
+-------------------------+--------------------------------------------------+

Resources/doc/filter/sort.rst

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ First, you have to specify the request field:
4545
+========================+======================================================================================+
4646
| `request_field` | Request field used to pass the sort choice id (e.g. `www.page.com/?request_field=4`) |
4747
+------------------------+--------------------------------------------------------------------------------------+
48+
| `tags` | Array of filter specific tags that will be accessible at Twig view data. |
49+
+------------------------+--------------------------------------------------------------------------------------+
4850

4951
After which you can specify multiple sort options/choices:
5052

@@ -102,6 +104,10 @@ View data returned by this filter to be used in template:
102104
+-------------------------+--------------------------------------------------+
103105
| getChoices() | Returns a list of available sort choices |
104106
+-------------------------+--------------------------------------------------+
107+
| getTags() | Lists all tags specified at filter configuration |
108+
+-------------------------+--------------------------------------------------+
109+
| hasTag($tag) | Checks if filter has the specific tag |
110+
+-------------------------+--------------------------------------------------+
105111

106112

107113
Each choice has its own data:

Search/FiltersManager.php

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ protected function getFiltersViewData(DocumentIterator $result, SearchRequest $r
149149
$viewData->setName($name);
150150
$viewData->setUrlParameters($this->composeUrlParameters($request, $filter));
151151
$viewData->setState($request->get($name));
152+
$viewData->setTags($filter->getTags());
152153
$viewData->setResetUrlParameters($this->composeUrlParameters($request, $filter, [$name]));
153154
$out[$name] = $filter->getViewData($result, $viewData);
154155
}

Tests/Functional/DependencyInjection/ServiceCreationTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function getTestServicesData()
5050
[
5151
'getField' => 'price',
5252
'getRequestField' => 'range',
53+
'getTags' => ['badged', 'permanent'],
5354
],
5455
],
5556
[
@@ -58,6 +59,7 @@ public function getTestServicesData()
5859
[
5960
'getField' => 'choice',
6061
'getRequestField' => 'choice',
62+
'getTags' => ['badged'],
6163
],
6264
],
6365
[

Tests/Functional/Filters/Widget/Choice/MultiTermChoiceTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected function getFilterManager()
9595

9696
$filter = new MultiTermChoice();
9797
$filter->setRequestField('choice');
98+
$filter->setTags(['badged']);
9899
$filter->setField('color');
99100
$container->set('choice', $filter);
100101

@@ -185,6 +186,7 @@ public function testChoiceUrl(Request $request, array $expectedUrlParams)
185186
}
186187
}
187188

189+
$this->assertTrue($viewData->hasTag('badged'));
188190
$this->assertEquals($expectedUrlParams, $actualUrls);
189191
}
190192
}

Tests/Functional/Filters/Widget/Choice/SingleTermChoiceTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function testChoicesSort($sortParams, $expectedChoices)
139139

140140
$filter = new SingleTermChoice();
141141
$filter->setRequestField('choice');
142+
$filter->setTags(['tagged']);
142143
$filter->setField('color');
143144
$filter->setSortType($sortParams);
144145

@@ -155,6 +156,7 @@ public function testChoicesSort($sortParams, $expectedChoices)
155156
$actualChoices[] = $choice->getLabel();
156157
}
157158

159+
$this->assertFalse($result->hasTag('badged'));
158160
$this->assertEquals($expectedChoices, $actualChoices);
159161
}
160162
}

Tests/Unit/DependencyInjection/ConfigurationTest.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,27 @@ protected function getBaseConfiguration()
4747
'exclude' => ['size'],
4848
],
4949
],
50+
'tags' => [],
5051
],
5152
],
5253
'sort' => [
5354
'sorting' => [
5455
'request_field' => 'sort',
5556
'choices' => [],
57+
'tags' => [],
5658
],
5759
],
5860
'pager' => [
59-
'paging' => ['request_field' => 'page'],
61+
'paging' => ['request_field' => 'page', 'tags' => []],
6062
],
6163
'range' => [
62-
'range' => ['request_field' => 'range'],
64+
'range' => ['request_field' => 'range', 'tags' => []],
6365
],
6466
'date_range' => [
65-
'date' => ['request_field' => 'date_range', 'field' => 'date'],
67+
'date' => ['request_field' => 'date_range', 'field' => 'date', 'tags' => []],
6668
],
6769
'choice' => [
68-
'single_choice' => ['request_field' => 'choice'],
70+
'single_choice' => ['request_field' => 'choice', 'tags' => ['badged']],
6971
],
7072
],
7173
];
@@ -85,7 +87,9 @@ public function getTestConfigurationData()
8587
$expectedBaseConfig['filters']['pager']['paging']['count_per_page'] = 10;
8688
$expectedBaseConfig['filters']['pager']['paging']['max_pages'] = 8;
8789
$expectedBaseConfig['filters']['document_field'] = [];
88-
$expectedBaseConfig['filters']['choice'] = ['single_choice' => ['request_field' => 'choice']];
90+
$expectedBaseConfig['filters']['choice'] = [
91+
'single_choice' => ['request_field' => 'choice', 'tags' => ['badged']],
92+
];
8993
$expectedBaseConfig['filters']['multi_choice'] = [];
9094
$expectedBaseConfig['filters']['fuzzy'] = [];
9195

@@ -108,7 +112,7 @@ public function getTestConfigurationData()
108112
$customConfig = $expectedBaseConfig;
109113
$customConfig['filters']['sort']['sorting']['choices'][0] = ['field' => 'test'];
110114
$expectedConfig = $customConfig;
111-
$expectedConfig['filters']['choice'] = ['single_choice' => ['request_field' => 'choice']];
115+
$expectedConfig['filters']['choice'] = ['single_choice' => ['request_field' => 'choice', 'tags' => ['badged']]];
112116
$expectedConfig['filters']['sort']['sorting']['choices'][0]['label'] = 'test';
113117
$expectedConfig['filters']['sort']['sorting']['choices'][0]['default'] = false;
114118
$expectedConfig['filters']['sort']['sorting']['choices'][0]['order'] = 'asc';

Tests/Unit/DependencyInjection/ONGRFilterManagerExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public function testMethodCalls()
215215
'field' => 'page_field',
216216
'count_per_page' => 10,
217217
'max_pages' => 8,
218+
'tags' => [],
218219
];
219220

220221
$config['ongr_filter_manager']['filters'] = [

Tests/app/config/config_test.yml

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ ongr_filter_manager:
6666
range:
6767
request_field: 'range'
6868
field: 'price'
69+
tags:
70+
- badged
71+
- permanent
6972
date_range:
7073
date:
7174
request_field: 'date_range'
@@ -75,6 +78,8 @@ ongr_filter_manager:
7578
request_field: 'choice'
7679
field: 'choice'
7780
size: 2
81+
tags:
82+
- badged
7883
choice:
7984
single_choice:
8085
request_field: 'single_choice'

0 commit comments

Comments
 (0)