src/V4/Form/Type/QuoteLineInfo/QuoteLineInfoType.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\V4\Form\Type\QuoteLineInfo;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use App\Form\Type\SpecificFieldType;
  5. use App\Form\Type\SubresourceChoicesTrait;
  6. use App\Model\Product\Product;
  7. use App\Security\SecurityConfig;
  8. use App\Service\Cache\CacheManager;
  9. use App\V4\EventSubscriber\QuoteLineInfoProductAttributeFormEventSubscriber;
  10. use App\V4\Form\AsyncSubresourceChoicesLoader;
  11. use App\V4\Form\Type\QuoteLineInfoProductAttribute\QuoteLineInfoProductAttributeType;
  12. use App\V4\Logger\SentryLogger;
  13. use App\V4\Model\QuoteLineInfo\QuoteLineInfo;
  14. use Psr\Cache\CacheException;
  15. use Psr\Cache\InvalidArgumentException;
  16. use Symfony\Component\Form\Event\PreSubmitEvent;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. use Symfony\Component\Form\FormEvents;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Component\Validator\Constraints\Valid;
  24. class QuoteLineInfoType extends SpecificFieldType
  25. {
  26.     use SubresourceChoicesTrait {
  27.         getEntityChoices as private getEntityChoicesTrait;
  28.     }
  29.     /**
  30.      * @var CollectionDataProviderInterface
  31.      */
  32.     private $collectionDataProvider;
  33.     /**
  34.      * @var CacheManager
  35.      */
  36.     private $cacheManager;
  37.     /**
  38.      * @var Security
  39.      */
  40.     private $security;
  41.     /**
  42.      * @var QuoteLineInfoProductAttributeFormEventSubscriber
  43.      */
  44.     private $quoteLineInfoProductAttributeFormEventSubscriber;
  45.     /**
  46.      * @var SentryLogger
  47.      */
  48.     private $sentryLogger;
  49.     /**
  50.      * @param CollectionDataProviderInterface                  $collectionDataProvider
  51.      * @param CacheManager                                     $cacheManager
  52.      * @param Security                                         $security
  53.      * @param QuoteLineInfoProductAttributeFormEventSubscriber $quoteLineInfoProductAttributeFormEventSubscriber
  54.      * @param SentryLogger                                     $sentryLogger
  55.      */
  56.     public function __construct(
  57.         CollectionDataProviderInterface $collectionDataProvider,
  58.         CacheManager $cacheManager,
  59.         Security $security,
  60.         QuoteLineInfoProductAttributeFormEventSubscriber $quoteLineInfoProductAttributeFormEventSubscriber,
  61.         SentryLogger $sentryLogger
  62.     ) {
  63.         $this->collectionDataProvider $collectionDataProvider;
  64.         $this->cacheManager $cacheManager;
  65.         $this->security $security;
  66.         $this->quoteLineInfoProductAttributeFormEventSubscriber $quoteLineInfoProductAttributeFormEventSubscriber;
  67.         $this->sentryLogger $sentryLogger;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function buildForm(FormBuilderInterface $builder, array $options): void
  73.     {
  74.         $builder
  75.             ->add('product'ChoiceType::class, [
  76.                 'choice_value' => 'id',
  77.                 'choice_label' => 'name',
  78.                 'choice_loader' => new AsyncSubresourceChoicesLoader(
  79.                     $this->collectionDataProvider,
  80.                     $this->cacheManager,
  81.                     $this->sentryLogger,
  82.                     Product::class
  83.                 ),
  84.             ]);
  85.         if ($this->security->isGranted(SecurityConfig::MODULE_PRODUCT_CUSTOMIZE)) {
  86.             $builder->addEventSubscriber($this->quoteLineInfoProductAttributeFormEventSubscriber);
  87.             $builder
  88.                 ->add('quoteLineInfoProductAttributes'CollectionType::class, [
  89.                     'label' => 'quote_line_info_product_attributes',
  90.                     'entry_type' => QuoteLineInfoProductAttributeType::class,
  91.                     'entry_options' => [
  92.                         'allow_extra_fields' => $options['allow_extra_fields'] ?? false,
  93.                     ],
  94.                     'prototype' => true,
  95.                     'prototype_name' => 'quote_line_info_product_attributes__value__',
  96.                     'allow_add' => true,
  97.                     'allow_delete' => true,
  98.                     'by_reference' => true,
  99.                     'delete_empty' => true,
  100.                     'required' => false,
  101.                     'constraints' => [new Valid()],
  102.                     'attr' => [
  103.                         'data-hide-from-vieworders' => true,
  104.                     ],
  105.                 ]);
  106.             $builder->addEventListener(FormEvents::PRE_SUBMIT, function (PreSubmitEvent $event) {
  107.                 $quoteLineInfoType $event->getData();
  108.                 if ('[]' === $event->getData()['quoteLineInfoProductAttributes']) {
  109.                     $quoteLineInfoType['quoteLineInfoProductAttributes'] = [];
  110.                     $event->setData($quoteLineInfoType);
  111.                 }
  112.             });
  113.         }
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function configureOptions(OptionsResolver $resolver): void
  119.     {
  120.         $resolver->setDefaults([
  121.             'data_class' => QuoteLineInfo::class,
  122.             'required' => false,
  123.         ]);
  124.     }
  125.     /**
  126.      * @param string $entityFQCN
  127.      *
  128.      * @return array<string, object>
  129.      *
  130.      * @throws CacheException
  131.      * @throws InvalidArgumentException
  132.      */
  133.     private function getEntityChoices(string $entityFQCN): array
  134.     {
  135.         return $this->getEntityChoicesTrait(
  136.             $this->collectionDataProvider,
  137.             $this->cacheManager,
  138.             $this->sentryLogger,
  139.             $entityFQCN
  140.         );
  141.     }
  142. }