33namespace Loki \AdminComponents \Component \Grid ;
44
55use Loki \AdminComponents \Grid \Column \Column ;
6+ use Loki \AdminComponents \Grid \Filter \FilterFactory ;
67use Magento \Framework \Data \OptionSourceInterface ;
78use Magento \Framework \DataObject ;
89use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
1415use Loki \AdminComponents \Grid \Cell \CellActionFactory ;
1516use Loki \AdminComponents \Grid \Cell \CellTemplateResolver ;
1617use Loki \AdminComponents \Grid \ColumnLoader ;
17- use Loki \AdminComponents \Grid \FilterInterface ;
18+ use Loki \AdminComponents \Grid \Filter \ FilterInterface ;
1819use Loki \AdminComponents \Grid \MassAction \MassActionInterface ;
1920use Loki \AdminComponents \Grid \State ;
2021use Loki \AdminComponents \Grid \StateManager ;
2122use Loki \AdminComponents \Ui \Button ;
2223use Loki \AdminComponents \Ui \ButtonFactory ;
2324use Loki \Components \Component \ComponentViewModel ;
2425use Loki \Components \Util \CamelCaseConvertor ;
26+ use RuntimeException ;
2527
2628/**
2729 * @method GridRepository getRepository()
@@ -39,7 +41,8 @@ public function __construct(
3941 protected ColumnLoader $ columnLoader ,
4042 protected ObjectManagerInterface $ objectManager ,
4143 protected ButtonFactory $ buttonFactory ,
42- protected FieldFactory $ fieldFactory
44+ protected FieldFactory $ fieldFactory ,
45+ protected FilterFactory $ filterFactory ,
4346 ) {
4447 }
4548
@@ -138,6 +141,7 @@ public function getJsData(): array
138141 return [
139142 ...parent ::getJsData (),
140143 ...$ this ->getState ()->toArray (),
144+ 'gridFilters ' => $ this ->getGridFilterValues (),
141145 'columnPositions ' => $ this ->getColumnPositions (),
142146 'newUrl ' => $ this ->getNewUrl (),
143147 'indexUrl ' => $ this ->getIndexUrl (),
@@ -285,6 +289,34 @@ public function getRowAction(DataObject $item): CellAction
285289 return $ this ->cellActionFactory ->create ($ editUrl , 'Edit ' );
286290 }
287291
292+ /**
293+ * @return FilterInterface[]
294+ */
295+ public function getGridFilters (): array
296+ {
297+ $ gridFilters = [];
298+ $ gridFilterDefinitions = (array )$ this ->getBlock ()->getGridFilters ();
299+ if (empty ($ gridFilterDefinitions )) {
300+ return [];
301+ }
302+
303+ foreach ($ gridFilterDefinitions as $ gridFilterDefinition ) {
304+ $ gridFilters [] = $ this ->filterFactory ->createFromArray ($ gridFilterDefinition );
305+ }
306+
307+ return $ gridFilters ;
308+ }
309+
310+ public function getGridFilterValues (): array
311+ {
312+ $ gridFilterValues = [];
313+ foreach ($ this ->getGridFilters () as $ gridFilter ) {
314+ $ value = $ this ->getState ()->getFilterValue ($ gridFilter ->getCode ());
315+ $ gridFilterValues [$ gridFilter ->getCode ()] = $ value ;
316+ }
317+
318+ return $ gridFilterValues ;
319+ }
288320
289321 /**
290322 * @return Field[]
@@ -305,7 +337,7 @@ public function getFilterFields(): array
305337 }
306338
307339 if (isset ($ filter ['options ' ]) && false === $ filter ['options ' ] instanceof OptionSourceInterface) {
308- throw new \ RuntimeException ('Filter uses wrong options class ' );
340+ throw new RuntimeException ('Filter uses wrong options class ' );
309341 }
310342
311343 $ field = $ this ->fieldFactory ->createWithBlock (
0 commit comments