src/EventSubscriber/AddFormFieldLocationTransactionSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\Form\FormEvent;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\FormEvents;
  8. use App\Form\Type\ContactTenantChoiceType;
  9. use App\Infrastructure\RequestDto\ContactDtoRequest;
  10. use App\Infrastructure\RequestDto\Reference\ReferencePropertyServiceTypeDto;
  11. class AddFormFieldLocationTransactionSubscriber 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_RENTAL,
  31.                     ReferencePropertyServiceTypeDto::REFERENCE_CODE_PROPERTY_SERVICE_TYPE_SEARCH_RENT],
  32.                 true
  33.             )
  34.         ) {
  35.             return;
  36.         }
  37.         $form->add('contact'ContactTenantChoiceType::class, [
  38.             'placeholder' => $this->translator->trans('transaction.contact.placeholder', [], 'transaction'),
  39.             'label' => $this->translator->trans(
  40.                 'transaction.properties.tenantContact',
  41.                 [],
  42.                 'transaction'
  43.             ),
  44.             'label_attr' => [
  45.                 'class' => 'form-label font-weight-bold text-uppercase'
  46.             ],
  47.             'required' => true,
  48.             'data' => $transaction $transaction->getContact() : null,
  49.             'choice_label' => function (ContactDtoRequest $contact) {
  50.                 return $contact->getFullName();
  51.             },
  52.             'attr' => [
  53.                 'class' => 'form-control',
  54.                 'required' => 'required'
  55.             ],
  56.         ]);
  57.     }
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             FormEvents::PRE_SET_DATA => 'onPreSetData'
  62.         ];
  63.     }
  64. }