Si entendí bien, pues lo pudieras solucionar creando un nuevo Type, digamos:
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\Form\EntityToIntTransformer;
class EntityIdType extends AbstractType
{
private $om;
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new EntityToIntTransformer($this->om, $options['data_class']);
$builder->addModelTransformer($transformer);
// $transformer = new EntityToIntTransformer(
// $this->om, $options['data_class']
// );
//$builder->addModelTransformer($transformer);
}
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
{
parent::configureOptions($resolver);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'La entidad no existe.'
));
}
public function getParent()
{
return \Symfony\Component\Form\Extension\Core\Type\HiddenType::class;
}
}
Lo registras como servicio
services:
app.form.type.entity_id:
class: AppBundle\Form\Type\EntityIdType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- {name: form.type, alias: entity_id }
y en el formulario pues lo empleas como un widget más.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form->add('embarazada', EntityIdType::class, array(
'data_class' => 'AppBundle\Entity\EntityConcreta',
'empty_data' => null
));
}