11
11
use Magento \Framework \App \RequestInterface ;
12
12
use Magento \Framework \View \Element \UiComponentInterface ;
13
13
use Magento \Framework \Data \Collection \AbstractDb ;
14
+ use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
14
15
15
16
/**
16
17
* Class Filter
@@ -43,6 +44,11 @@ class Filter
43
44
*/
44
45
protected $ filterBuilder ;
45
46
47
+ /**
48
+ * @var DataProviderInterface
49
+ */
50
+ private $ dataProvider ;
51
+
46
52
/**
47
53
* @param UiComponentFactory $factory
48
54
* @param RequestInterface $request
@@ -74,23 +80,22 @@ public function getComponent()
74
80
}
75
81
76
82
/**
83
+ * Adds filters to collection using DataProvider filter results
84
+ *
77
85
* @param AbstractDb $collection
78
86
* @return AbstractDb
79
87
* @throws LocalizedException
80
88
*/
81
89
public function getCollection (AbstractDb $ collection )
82
90
{
83
- $ component = $ this ->getComponent ();
84
- $ this ->prepareComponent ($ component );
85
- $ dataProvider = $ component ->getContext ()->getDataProvider ();
86
- $ dataProvider ->setLimit (0 , false );
87
- $ ids = [];
88
- foreach ($ dataProvider ->getSearchResult ()->getItems () as $ document ) {
89
- $ ids [] = $ document ->getId ();
91
+ $ idsArray = $ this ->getFilterIds ();
92
+ if (!empty ($ idsArray )) {
93
+ $ collection ->addFieldToFilter (
94
+ $ collection ->getIdFieldName (),
95
+ ['in ' => $ idsArray ]
96
+ );
90
97
}
91
-
92
- $ collection ->addFieldToFilter ($ collection ->getIdFieldName (), ['in ' => $ ids ]);
93
- return $ this ->applySelection ($ collection );
98
+ return $ collection ;
94
99
}
95
100
96
101
/**
@@ -106,9 +111,7 @@ public function applySelectionOnTargetProvider()
106
111
if ('false ' === $ excluded ) {
107
112
return ;
108
113
}
109
- $ component = $ this ->getComponent ();
110
- $ this ->prepareComponent ($ component );
111
- $ dataProvider = $ component ->getContext ()->getDataProvider ();
114
+ $ dataProvider = $ this ->getDataProvider ();
112
115
try {
113
116
if (is_array ($ excluded ) && !empty ($ excluded )) {
114
117
$ this ->filterBuilder ->setConditionType ('nin ' )
@@ -127,6 +130,8 @@ public function applySelectionOnTargetProvider()
127
130
}
128
131
129
132
/**
133
+ * Applies selection to collection from POST parameters
134
+ *
130
135
* @param AbstractDb $collection
131
136
* @return AbstractDb
132
137
* @throws LocalizedException
@@ -169,7 +174,7 @@ public function prepareComponent(UiComponentInterface $component)
169
174
}
170
175
171
176
/**
172
- * Returns RefererUrl
177
+ * Returns Referrer Url
173
178
*
174
179
* @return string|null
175
180
*/
@@ -178,4 +183,33 @@ public function getComponentRefererUrl()
178
183
$ data = $ this ->getComponent ()->getContext ()->getDataProvider ()->getConfigData ();
179
184
return (isset ($ data ['referer_url ' ])) ? $ data ['referer_url ' ] : null ;
180
185
}
186
+
187
+ /**
188
+ * Get data provider
189
+ *
190
+ * @return DataProviderInterface
191
+ */
192
+ private function getDataProvider ()
193
+ {
194
+ if (!$ this ->dataProvider ) {
195
+ $ component = $ this ->getComponent ();
196
+ $ this ->prepareComponent ($ component );
197
+ $ this ->dataProvider = $ component ->getContext ()->getDataProvider ();
198
+ }
199
+ return $ this ->dataProvider ;
200
+ }
201
+
202
+ /**
203
+ * Get filter ids as array
204
+ *
205
+ * @return int[]
206
+ */
207
+ private function getFilterIds ()
208
+ {
209
+ $ this ->applySelectionOnTargetProvider ();
210
+ if ($ this ->getDataProvider ()->getSearchResult ()) {
211
+ return $ this ->getDataProvider ()->getSearchResult ()->getAllIds ();
212
+ }
213
+ return [];
214
+ }
181
215
}
0 commit comments