You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.2KB

  1. <?php
  2. namespace Lc\SovBundle\Form\Common;
  3. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EaFormPanelType;
  4. use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormInterface;
  8. use Symfony\Component\Form\FormView;
  9. use Symfony\Component\OptionsResolver\Options;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. /**
  12. * Custom form type that deals with some of the logic used to render the
  13. * forms used to create and edit EasyAdmin entities.
  14. *
  15. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  16. */
  17. class CrudFormType extends AbstractType
  18. {
  19. protected $parent;
  20. protected $doctrineOrmTypeGuesser;
  21. public function __construct(
  22. DoctrineOrmTypeGuesser $doctrineOrmTypeGuesser,
  23. \EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType $crudFormType
  24. ) {
  25. $this->parent = $crudFormType;
  26. $this->doctrineOrmTypeGuesser = $doctrineOrmTypeGuesser;
  27. }
  28. public function buildForm(FormBuilderInterface $builder, array $options)
  29. {
  30. $this->parent->buildForm($builder, $options);
  31. $entityDto = $options['entityDto'];
  32. $formPanels = [];
  33. $currentFormPanel = 0;
  34. foreach ($entityDto->getFields() as $fieldDto) {
  35. if (null === $formFieldType = $fieldDto->getFormType()) {
  36. $guessType = $this->doctrineOrmTypeGuesser->guessType($entityDto->getFqcn(), $fieldDto->getProperty());
  37. $formFieldType = $guessType->getType();
  38. $formFieldOptions = array_merge($guessType->getOptions(), $formFieldOptions);
  39. }
  40. if (EaFormPanelType::class === $formFieldType) {
  41. ++$currentFormPanel;
  42. $formPanels[$currentFormPanel] = [
  43. 'form_tab' => $currentFormTab ?? null,
  44. 'label' => $fieldDto->getLabel(),
  45. 'help' => $fieldDto->getHelp(),
  46. 'css_class' => $fieldDto->getCssClass(),
  47. ];
  48. foreach ($fieldDto->getCustomOptions()->all() as $customOptionKey => $customOption) {
  49. $formPanels[$currentFormPanel][$customOptionKey] = $customOption;
  50. }
  51. continue;
  52. }
  53. }
  54. $builder->setAttribute('ea_form_panels', $formPanels);
  55. //$this->niche->buildForm($builder, $options);
  56. }
  57. public function finishView(FormView $view, FormInterface $form, array $options)
  58. {
  59. $view->vars['translation_entity_name'] = $options['translation_entity_name'];
  60. $this->parent->finishView($view, $form, $options);
  61. }
  62. public function configureOptions(OptionsResolver $resolver)
  63. {
  64. $this->parent->configureOptions($resolver);
  65. $resolver->setDefaults(
  66. [
  67. 'translation_entity_name' => static function (Options $options, $dataClass) {
  68. return $dataClass ?? $options['entityDto']->getFqcn();
  69. }
  70. ]
  71. );
  72. }
  73. public function getBlockPrefix()
  74. {
  75. return $this->parent->getBlockPrefix();
  76. }
  77. }