src/Form/PublicationType.php line 37
<?php
namespace App\Form;
use App\Entity\Journal;
use App\Entity\Keyword;
use App\Entity\KeywordPlus;
use App\Entity\Publication;
use App\Entity\Publisher;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Repository\PublisherRepository;
use Doctrine\ORM\EntityManagerInterface;
class PublicationType extends AbstractType
{
public function __construct(private EntityManagerInterface $entityManager) {
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$publicationTypeChoices = (new Publication)->getPublicationTypeChoices();
$builder
//->add('publicationAuthors')
//->add('journal')
->add('publicationAuthors', CollectionType::class, [
'entry_type' => PublicationAuthorType::class,
'entry_options' => ['label' => false],
'allow_delete' => true
])
->add('interdisciplinary', null, ['label' => 'Interdisciplinaria'])
->add('validated', ChoiceType::class, [
'choices' => [
'Validada' => true,
'Por Validar' => false,
],
'label' => 'Estado Validación'
])
->add('publisher', EntityType::class, [
'class' => Publisher::class,
'choices' => [],
'required' => false,
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
// Check if the current publication has a publisher
if ($data && $data->getPublisher()) {
$publisher = $data->getPublisher();
$form->add('publisher', EntityType::class, [
'class' => Publisher::class,
'choices' => [$publisher], // Add the current publisher as the initial choice
'data' => $publisher, // Pre-select the publisher
'required' => false,
]);
}
})
->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event){
$form = $event->getForm();
$publisher = $event->getData()['publisher'];
if($publisher){
// Use the repository to find the Publisher entity by its ID
$publisherRepository = $this->entityManager->getRepository(Publisher::class);
$publisherAsObject = $publisherRepository->find($publisher);
$form->add('publisher', EntityType::class, [
'class' => Publisher::class,
'choices' => [$publisherAsObject],
]);
}
})
->add('privateComments', null, ['label' => 'Comentarios Privados'])
->add('publicComments', null, ['label' => 'Comentarios Públicos'])
->add('publicationType', ChoiceType::class, [
'choices' => $publicationTypeChoices,
'label' => 'Tipo de Publicación'
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Publication::class,
]);
}
}