vendor/bitbag/elasticsearch-plugin/src/Form/Type/SearchFacetsType.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3. * This file has been created by developers from BitBag.
  4. * Feel free to contact us once you face any issues or want to start
  5. * another great project.
  6. * You can find more information about us on https://bitbag.io and write us
  7. * an email on hello@bitbag.io.
  8. */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Form\Type;
  11. use BitBag\SyliusElasticsearchPlugin\Facet\RegistryInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Model\SearchFacets;
  13. use Symfony\Component\Form\AbstractType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. final class SearchFacetsType extends AbstractType
  18. {
  19. private RegistryInterface $facetRegistry;
  20. public function __construct(RegistryInterface $facetRegistry)
  21. {
  22. $this->facetRegistry = $facetRegistry;
  23. }
  24. public function buildForm(FormBuilderInterface $builder, array $options): void
  25. {
  26. foreach ($options['facets'] as $facetId => $facetData) {
  27. $facet = $this->facetRegistry->getFacetById($facetId);
  28. $choices = [];
  29. foreach ($facetData['buckets'] as $bucket) {
  30. $choices[$facet->getBucketLabel($bucket)] = $bucket['key'];
  31. }
  32. if (!empty($choices)) {
  33. $builder
  34. ->add(
  35. $facetId,
  36. ChoiceType::class,
  37. [
  38. 'label' => $facet->getLabel(),
  39. 'choices' => $choices,
  40. 'expanded' => true,
  41. 'multiple' => true,
  42. ]
  43. )
  44. ;
  45. }
  46. }
  47. }
  48. public function configureOptions(OptionsResolver $resolver): void
  49. {
  50. $resolver->setRequired('facets');
  51. $resolver->setDefault('data_class', SearchFacets::class);
  52. }
  53. }