src/Cms/FileBundle/Form/Type/FileModalType.php line 21

Open in your IDE?
  1. <?php
  2. namespace Cms\FileBundle\Form\Type;
  3. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  4. use Cms\FileBundle\Controller\ModalController;
  5. use Cms\FileBundle\Doctrine\Nodes\FileRepository;
  6. use Cms\FileBundle\Entity\Nodes\File;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\CallbackTransformer;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\Form\FormView;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. /**
  15.  * Class FileModalType
  16.  * @package Cms\FileBundle\Form\Type
  17.  */
  18. class FileModalType extends AbstractType
  19. {
  20.     /**
  21.      * @var FileRepository
  22.      */
  23.     protected $repFileRepository;
  24.     /**
  25.      * @param EntityManager $em
  26.      */
  27.     public function __construct(EntityManager $em)
  28.     {
  29.         $this->repFileRepository $em->getRepository(File::class);
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function buildView(FormView $viewFormInterface $form, array $options)
  35.     {
  36.         $view->vars['mode'] = $options['modal_mode'];
  37.         $view->vars['host'] = $options['host'];
  38.         $view->vars['container'] = $options['container'];
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function configureOptions(OptionsResolver $resolver)
  44.     {
  45.         $resolver->setRequired(array(
  46.             'container',
  47.         ));
  48.         $resolver->setDefaults(array(
  49.             'modal_mode' => ModalController::MODES__ID,
  50.             'host' => null,
  51.         ));
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function buildForm(FormBuilderInterface $builder, array $options)
  57.     {
  58.         if ($options['modal_mode'] === ModalController::MODES__ID) {
  59.             $builder->addModelTransformer(
  60.                 new CallbackTransformer(
  61.                     function (File $original null) {
  62.                         if ( ! empty($original)) {
  63.                             return $original->getId();
  64.                         }
  65.                         return null;
  66.                     },
  67.                     function ($submitted null) {
  68.                         if ( ! empty($submitted)) {
  69.                             return $this->repFileRepository->findExact(intval($submitted));
  70.                         }
  71.                         return null;
  72.                     }
  73.                 )
  74.             );
  75.         }
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      * This should be treated as a hidden form type, as its display is rather complex and is handled specially.
  80.      */
  81.     public function getParent(): ?string
  82.     {
  83.         return TextType::class;
  84.     }
  85. }