15
15
namespace Gally \SyliusPlugin \Form \Type \Filter ;
16
16
17
17
use Gally \SyliusPlugin \Event \GridFilterUpdateEvent ;
18
+ use Gally \SyliusPlugin \Grid \Filter \Type \SelectFilterType ;
18
19
use Gally \SyliusPlugin \Search \Aggregation \Aggregation ;
19
20
use Gally \SyliusPlugin \Search \Aggregation \AggregationOption ;
20
21
use Sylius \Bundle \GridBundle \Form \Type \Filter \BooleanFilterType ;
21
- use Sylius \Bundle \GridBundle \Form \Type \Filter \SelectFilterType ;
22
+ use Sylius \Bundle \TaxonomyBundle \Doctrine \ORM \TaxonRepository ;
23
+ use Sylius \Component \Grid \Parameters ;
24
+ use Sylius \Component \Locale \Context \LocaleContextInterface ;
22
25
use Symfony \Component \Form \AbstractType ;
23
26
use Symfony \Component \Form \Extension \Core \Type \RangeType ;
24
27
use Symfony \Component \Form \FormBuilderInterface ;
28
+ use Symfony \Component \HttpFoundation \RequestStack ;
29
+ use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
25
30
26
31
class GallyDynamicFilterType extends AbstractType
27
32
{
33
+ public function __construct (
34
+ private UrlGeneratorInterface $ router ,
35
+ private RequestStack $ requestStack ,
36
+ private TaxonRepository $ taxonRepository ,
37
+ private LocaleContextInterface $ localeContext ,
38
+ ) {
39
+ }
40
+
28
41
/**
29
42
* @var Aggregation[]
30
43
*/
@@ -78,16 +91,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
78
91
/** @var AggregationOption $option */
79
92
$ choices [$ option ->getLabel ()] = $ option ->getId ();
80
93
}
94
+ $ options = [
95
+ 'block_prefix ' => 'sylius_gally_filter_checkbox ' ,
96
+ 'label ' => $ aggregation ->getLabel (),
97
+ 'choices ' => $ choices ,
98
+ 'expanded ' => true ,
99
+ 'multiple ' => true ,
100
+ ];
101
+ if ($ aggregation ->hasMore ()) {
102
+ $ options ['has_more_url ' ] = $ this ->buildHasMoreUrl ($ aggregation ->getField ());
103
+ }
81
104
$ builder ->add (
82
105
$ aggregation ->getField (),
83
106
SelectFilterType::class,
84
- [
85
- 'block_prefix ' => 'sylius_gally_filter_checkbox ' ,
86
- 'label ' => $ aggregation ->getLabel (),
87
- 'choices ' => $ choices ,
88
- 'expanded ' => true ,
89
- 'multiple ' => true ,
90
- ]
107
+ $ options
91
108
);
92
109
break ;
93
110
default :
@@ -100,4 +117,24 @@ public function onFilterUpdate(GridFilterUpdateEvent $event): void
100
117
{
101
118
$ this ->aggregations = $ event ->getAggregations ();
102
119
}
120
+
121
+ private function buildHasMoreUrl (string $ field ): string
122
+ {
123
+ $ request = $ this ->requestStack ->getCurrentRequest ();
124
+ $ parameters = new Parameters ($ request ->query ->all ());
125
+ $ criteria = $ parameters ->get ('criteria ' , []);
126
+ $ search = (isset ($ criteria ['search ' ], $ criteria ['search ' ]['value ' ])) ? $ criteria ['search ' ]['value ' ] : '' ;
127
+ unset($ criteria ['search ' ]);
128
+ $ taxon = $ this ->taxonRepository ->findOneBySlug ($ request ->attributes ->get ('slug ' ), $ this ->localeContext ->getLocaleCode ());
129
+
130
+ return $ this ->router ->generate (
131
+ 'gally_filter_view_more_ajax ' ,
132
+ [
133
+ 'filterField ' => $ field ,
134
+ 'search ' => $ search ,
135
+ 'filters ' => $ criteria ,
136
+ 'taxon ' => $ taxon ->getId (),
137
+ ]
138
+ );
139
+ }
103
140
}
0 commit comments