src/Form/PublicationType.php line 37

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Journal;
  4. use App\Entity\Keyword;
  5. use App\Entity\KeywordPlus;
  6. use App\Entity\Publication;
  7. use App\Entity\Publisher;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use App\Repository\PublisherRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. class PublicationType extends AbstractType
  19. {
  20. public function __construct(private EntityManagerInterface $entityManager) {
  21. }
  22. public function buildForm(FormBuilderInterface $builder, array $options): void
  23. {
  24. $publicationTypeChoices = (new Publication)->getPublicationTypeChoices();
  25. $builder
  26. //->add('publicationAuthors')
  27. //->add('journal')
  28. ->add('publicationAuthors', CollectionType::class, [
  29. 'entry_type' => PublicationAuthorType::class,
  30. 'entry_options' => ['label' => false],
  31. 'allow_delete' => true
  32. ])
  33. ->add('interdisciplinary', null, ['label' => 'Interdisciplinaria'])
  34. ->add('validated', ChoiceType::class, [
  35. 'choices' => [
  36. 'Validada' => true,
  37. 'Por Validar' => false,
  38. ],
  39. 'label' => 'Estado Validación'
  40. ])
  41. ->add('publisher', EntityType::class, [
  42. 'class' => Publisher::class,
  43. 'choices' => [],
  44. 'required' => false,
  45. ])
  46. ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  47. $form = $event->getForm();
  48. $data = $event->getData();
  49. // Check if the current publication has a publisher
  50. if ($data && $data->getPublisher()) {
  51. $publisher = $data->getPublisher();
  52. $form->add('publisher', EntityType::class, [
  53. 'class' => Publisher::class,
  54. 'choices' => [$publisher], // Add the current publisher as the initial choice
  55. 'data' => $publisher, // Pre-select the publisher
  56. 'required' => false,
  57. ]);
  58. }
  59. })
  60. ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event){
  61. $form = $event->getForm();
  62. $publisher = $event->getData()['publisher'];
  63. if($publisher){
  64. // Use the repository to find the Publisher entity by its ID
  65. $publisherRepository = $this->entityManager->getRepository(Publisher::class);
  66. $publisherAsObject = $publisherRepository->find($publisher);
  67. $form->add('publisher', EntityType::class, [
  68. 'class' => Publisher::class,
  69. 'choices' => [$publisherAsObject],
  70. ]);
  71. }
  72. })
  73. ->add('privateComments', null, ['label' => 'Comentarios Privados'])
  74. ->add('publicComments', null, ['label' => 'Comentarios Públicos'])
  75. ->add('publicationType', ChoiceType::class, [
  76. 'choices' => $publicationTypeChoices,
  77. 'label' => 'Tipo de Publicación'
  78. ])
  79. ;
  80. }
  81. public function configureOptions(OptionsResolver $resolver): void
  82. {
  83. $resolver->setDefaults([
  84. 'data_class' => Publication::class,
  85. ]);
  86. }
  87. }