src/EventSubscriber/AddFormFieldSaleTransactionSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Form\FormEvent;
  6. use Symfony\Component\Form\FormEvents;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use App\Form\Type\ContactBuyerChoiceType;
  9. use App\Infrastructure\RequestDto\ContactDtoRequest;
  10. use App\Infrastructure\RequestDto\Reference\ReferencePropertyServiceTypeDto;
  11. class AddFormFieldSaleTransactionSubscriber implements EventSubscriberInterface
  12. {
  13.     private TranslatorInterface $translator;
  14.     /**
  15.      * @param TranslatorInterface $translator
  16.      */
  17.     public function __construct(
  18.         TranslatorInterface $translator
  19.     ) {
  20.         $this->translator $translator;
  21.     }
  22.     public function onPresetData(FormEvent $event): void
  23.     {
  24.         $transaction $event->getData();
  25.         $form $event->getForm();
  26.         if (
  27.             !$transaction
  28.             || !in_array(
  29.                 $transaction->getMandate()->getProperty()->getReferenceServiceType()->getCode(),
  30.                 [ReferencePropertyServiceTypeDto::REFERENCE_CODE_PROPERTY_SERVICE_TYPE_SELL,
  31.                     ReferencePropertyServiceTypeDto::REFERENCE_CODE_PROPERTY_SERVICE_TYPE_SEARCH_BUY],
  32.                 true
  33.             )
  34.         ) {
  35.             return;
  36.         }
  37.         $form->add('contact'ContactBuyerChoiceType::class, [
  38.             'placeholder' => $this->translator->trans(
  39.                 'transaction.contact.placeholder',
  40.                 [],
  41.                 'transaction'
  42.             ),
  43.             'label' => $this->translator->trans(
  44.                 'transaction.properties.buyerContact',
  45.                 [],
  46.                 'transaction'
  47.             ),
  48.             'label_attr' => [
  49.                 'class' => 'form-label font-weight-bold text-uppercase'
  50.             ],
  51.             'required' => true,
  52.             'data' => $transaction $transaction->getContact() : null,
  53.             'choice_label' => function (ContactDtoRequest $contact) {
  54.                 return $contact->getFullName();
  55.             },
  56.             'choice_attr' => function (?ContactDtoRequest $contact) {
  57.                 return $contact $contact->getRecommender()
  58.                         ? ['recommender' => $contact->getRecommender()->getFullName()] : [] : [];
  59.             },
  60.             'attr' => [
  61.                 'class' => 'form-control',
  62.                 'required' => 'required'
  63.             ]
  64.         ]);
  65.     }
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             FormEvents::PRE_SET_DATA => 'onPreSetData'
  70.         ];
  71.     }
  72. }