<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Infrastructure\RequestDto\Reference\ReferencePropertyServiceTypeDto;
class AddFormFieldApportSaleTransactionSubscriber 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('apport', TextType::class, [
'label' => $this->translator->trans(
'transaction.properties.apport',
[],
'transaction',
),
"required" => false,
'data' => $transaction->getApport() ?: null,
'attr' => [
'readonly' => false,
'disabled' => false,
'class' => 'form-control form-label mb-1',
'value' => $transaction->getApport() != null ? $transaction->getApport() : null
],
'label_attr' => [
'class' => 'form-label font-weight-bold text-uppercase'
],
]);
}
public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SET_DATA => 'onPreSetData'
];
}
}