7
7
8
8
namespace Magento \CatalogGraphQl \Model \Category ;
9
9
10
+ use Magento \Catalog \Api \Data \CategoryInterface ;
10
11
use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
12
+ use Magento \Catalog \Model \ResourceModel \Category \Collection ;
11
13
12
14
/**
13
15
* Category filter allows to filter collection using 'id, url_key, name' from search criteria.
@@ -32,24 +34,47 @@ public function __construct(
32
34
* Filter for filtering the requested categories id's based on url_key, ids, name in the result.
33
35
*
34
36
* @param array $args
35
- * @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
36
- * @return bool
37
+ * @param Collection $categoryCollection
37
38
*/
38
39
public function applyFilters (
39
40
array $ args ,
40
- \Magento \Catalog \Model \ResourceModel \Category \Collection $ categoryCollection
41
- ): bool {
41
+ Collection $ categoryCollection
42
+ ): void {
43
+ $ categoryCollection ->addAttributeToFilter (CategoryInterface::KEY_IS_ACTIVE , ['eq ' => 1 ]);
42
44
foreach ($ args ['filters ' ] as $ field => $ cond ) {
43
45
foreach ($ cond as $ condType => $ value ) {
44
46
if ($ field === 'ids ' ) {
45
47
$ categoryCollection ->addIdFilter ($ value );
46
- } elseif ($ condType === 'match ' ) {
47
- $ categoryCollection ->addAttributeToFilter ($ field , ['like ' => "% {$ value }% " ]);
48
48
} else {
49
- $ categoryCollection -> addAttributeToFilter ( $ field , [ $ condType => $ value] );
49
+ $ this -> addAttributeFilter ( $ categoryCollection , $ field , $ condType, $ value );
50
50
}
51
51
}
52
52
}
53
- return true ;
53
+ }
54
+
55
+ /**
56
+ * @param Collection $categoryCollection
57
+ * @param string $field
58
+ * @param string $condType
59
+ * @param string|array $value
60
+ */
61
+ private function addAttributeFilter ($ categoryCollection , $ field , $ condType , $ value )
62
+ {
63
+ if ($ condType === 'match ' ) {
64
+ $ this ->addMatchFilter ($ categoryCollection , $ field , $ value );
65
+ return ;
66
+ }
67
+ $ categoryCollection ->addAttributeToFilter ($ field , [$ condType => $ value ]);
68
+ }
69
+
70
+ /**
71
+ *
72
+ * @param Collection $categoryCollection
73
+ * @param string $field
74
+ * @param string $value
75
+ */
76
+ private function addMatchFilter ($ categoryCollection , $ field , $ value )
77
+ {
78
+ $ categoryCollection ->addAttributeToFilter ($ field , ['like ' => "% {$ value }% " ]);
54
79
}
55
80
}
0 commit comments