<?php
// src/Form/EventListener/AddNameFieldSubscriber.php
namespace App\FormSubscriber;
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\Component\Form\Form;
use Doctrine\ORM\EntityManagerInterface;
class RFEnvioCorrespondenciaSubscriber implements EventSubscriberInterface {
private $entityManager;
public function __construct(
EntityManagerInterface $entityManager
) {
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array {
// Tells the dispatcher that you want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return [FormEvents::PRE_SUBMIT => 'preSubmit'];
}
public function preSubmit(FormEvent $event): void {
$data = $event->getData();
if (isset($data['cliente'])) {
$idCliente = $data['cliente'];
$cliente = $this->entityManager->getRepository(\App\Entity\TerEmpresaCliente::class)->find($idCliente);
$data['direccionDestino'] = $cliente->getDireccion();
$event->setData($data);
}
}
}