vendor/bitbag/elasticsearch-plugin/src/Form/Type/ProductAttributesFilterType.php line 21

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\Context\ProductAttributesContextInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Form\Type\ChoiceMapper\ProductAttributesMapperInterface;
  13. use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. final class ProductAttributesFilterType extends AbstractFilterType
  17. {
  18.     private ProductAttributesContextInterface $productAttributesContext;
  19.     private ConcatedNameResolverInterface $attributeNameResolver;
  20.     private ProductAttributesMapperInterface $productAttributesMapper;
  21.     protected array $excludedAttributes;
  22.     public function __construct(
  23.         ProductAttributesContextInterface $productAttributesContext,
  24.         ConcatedNameResolverInterface $attributeNameResolver,
  25.         ProductAttributesMapperInterface $productAttributesMapper,
  26.         array $excludedAttributes
  27.     ) {
  28.         $this->productAttributesContext $productAttributesContext;
  29.         $this->attributeNameResolver $attributeNameResolver;
  30.         $this->productAttributesMapper $productAttributesMapper;
  31.         $this->excludedAttributes $excludedAttributes;
  32.     }
  33.     public function buildForm(FormBuilderInterface $builder, array $attributes): void
  34.     {
  35.         foreach ($this->productAttributesContext->getAttributes() as $productAttribute) {
  36.             if (in_array($productAttribute->getCode(), $this->excludedAttributes)) {
  37.                 continue;
  38.             }
  39.             $name $this->attributeNameResolver->resolvePropertyName($productAttribute->getCode());
  40.             $choices $this->productAttributesMapper->mapToChoices($productAttribute);
  41.             $choices array_unique($choices);
  42.             $builder->add($nameChoiceType::class, [
  43.                 'label' => $productAttribute->getName(),
  44.                 'required' => false,
  45.                 'multiple' => true,
  46.                 'expanded' => true,
  47.                 'choices' => $choices,
  48.             ]);
  49.         }
  50.     }
  51. }