<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Form\Type\Reference\ReferenceFinancingModeChoiceType;
use App\Infrastructure\RequestDto\Reference\ReferencePropertyServiceTypeDto;
class AddFormFieldFinanceModeSaleTransactionSubscriber implements EventSubscriberInterface
{
private TranslatorInterface $translator;
/**
* @param TranslatorInterface $translator
*/
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}
public function onPresetData(FormEvent $event): void
{
$transaction = $event->getData();
$form = $event->getForm();
if (!$transaction || $transaction->getMandate()->getProperty()->getReferenceServiceType()->getCode() !== ReferencePropertyServiceTypeDto::REFERENCE_CODE_PROPERTY_SERVICE_TYPE_SELL) {
return;
}
$form->add('referenceFinancingMode', ReferenceFinancingModeChoiceType::class, [
'label' => $this->translator->trans(
'transaction.properties.financeMode',
[],
'transaction'
),
'label_attr' => [
'class' => 'form-label font-weight-bold text-uppercase'
],
'required' => true,
'choice_label' => 'name',
'choice_value' => 'id',
'expanded' => false,
'multiple' => false,
'attr' => [
'class' => 'form-control mb-1'
]
]);
}
public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SET_DATA => 'onPreSetData'
];
}
}