2929use Symfony \Component \Form \Extension \Core \Type \RangeType ;
3030use Symfony \Component \Form \FormBuilderInterface ;
3131use Symfony \Component \HttpFoundation \RequestStack ;
32- use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
3332
3433class GallyDynamicFilterType extends AbstractType
3534{
3635 /**
3736 * @param TaxonRepository<TaxonInterface> $taxonRepository
3837 */
3938 public function __construct (
40- private UrlGeneratorInterface $ router ,
4139 private RequestStack $ requestStack ,
4240 private TaxonRepository $ taxonRepository ,
4341 private LocaleContextInterface $ localeContext ,
@@ -53,6 +51,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5351 {
5452 $ isTaxonPage = $ this ->isTaxonPage ();
5553 $ categoryFieldAdded = false ;
54+ $ facetContext = $ this ->buildFacetContext ();
5655
5756 if ($ isTaxonPage && !$ this ->hasCategoryAggregation ()) {
5857 // Gally doesn't always return a "category" aggregation on taxon listing pages (e.g. a
@@ -118,7 +117,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
118117 'choices ' => $ choices ,
119118 'expanded ' => true ,
120119 'multiple ' => true ,
121- 'search_url ' => $ this ->buildSearchUrl ($ aggregation ->getField ()),
120+ 'facet_search ' => $ facetContext ['search ' ],
121+ 'facet_filters ' => $ facetContext ['filters ' ],
122+ 'facet_taxon_code ' => $ facetContext ['taxonCode ' ],
122123 'has_more ' => $ aggregation ->hasMore (),
123124 ]
124125 );
@@ -225,7 +226,10 @@ private function getCurrentCategoryId(): ?string
225226 return \is_string ($ categoryId ) && '' !== $ categoryId ? $ categoryId : null ;
226227 }
227228
228- private function buildSearchUrl (string $ field ): string
229+ /**
230+ * @return array{search: ?string, filters: array<string, mixed>, taxonCode: ?string}
231+ */
232+ private function buildFacetContext (): array
229233 {
230234 $ request = $ this ->requestStack ->getCurrentRequest ();
231235 /** @var array<string, mixed> $queryParameters */
@@ -234,20 +238,18 @@ private function buildSearchUrl(string $field): string
234238 /** @var array<string, array<string, mixed>> $criteria */
235239 $ criteria = $ parameters ->get ('criteria ' , []);
236240 $ query = $ parameters ->get ('query ' , null );
237- $ search = (\is_string ($ query ) && '' !== $ query ) ? $ query : ((isset ($ criteria ['search ' ], $ criteria ['search ' ]['value ' ])) ? $ criteria ['search ' ]['value ' ] : '' );
238- unset($ criteria ['search ' ]);
241+ $ search = (\is_string ($ query ) && '' !== $ query ) ? $ query : ((isset ($ criteria ['search ' ], $ criteria ['search ' ]['value ' ]) && \is_string ($ criteria ['search ' ]['value ' ])) ? $ criteria ['search ' ]['value ' ] : null );
239242 /** @var string $slug */
240243 $ slug = $ request ?->attributes->get ('slug ' ) ?? '' ;
241244 $ taxon = $ this ->taxonRepository ->findOneBySlug ($ slug , $ this ->localeContext ->getLocaleCode ());
242245
243- return $ this ->router ->generate (
244- 'gally_filter_view_more_ajax ' ,
245- [
246- 'filterField ' => $ field ,
247- 'search ' => $ search ,
248- 'filters ' => $ criteria ,
249- 'taxon ' => $ taxon ?->getId(),
250- ]
251- );
246+ /** @var array<string, mixed> $filters */
247+ $ filters = \is_array ($ criteria ['gally ' ] ?? null ) ? $ criteria ['gally ' ] : [];
248+
249+ return [
250+ 'search ' => $ search ,
251+ 'filters ' => $ filters ,
252+ 'taxonCode ' => $ taxon ?->getCode(),
253+ ];
252254 }
253255}
0 commit comments