vendor/tetranz/select2entity-bundle/Form/Type/Select2EntityType.php line 24

Open in your IDE?
  1. <?php
  2. namespace Tetranz\Select2EntityBundle\Form\Type;
  3. use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
  4. use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;
  5. use Doctrine\Persistence\ObjectManager;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\DataTransformerInterface;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\Form\FormView;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. /**
  16.  *
  17.  * Class Select2EntityType
  18.  * @package Tetranz\Select2EntityBundle\Form\Type
  19.  */
  20. class Select2EntityType extends AbstractType
  21. {
  22.     /** @var ManagerRegistry */
  23.     protected $registry;
  24.     /** @var ObjectManager */
  25.     protected $em;
  26.     /** @var RouterInterface */
  27.     protected $router;
  28.     /** @var array */
  29.     protected $config;
  30.     /**
  31.      * @param ManagerRegistry   $registry
  32.      * @param RouterInterface   $router
  33.      * @param array             $config
  34.      */
  35.     public function __construct(ManagerRegistry $registryRouterInterface $router$config)
  36.     {
  37.         $this->registry $registry;
  38.         $this->em $registry->getManager();
  39.         $this->router $router;
  40.         $this->config $config;
  41.     }
  42.     public function buildForm(FormBuilderInterface $builder, array $options)
  43.     {
  44.         // custom object manager for this entity, override the default entity manager ?
  45.         if (isset($options['object_manager'])) {
  46.             $em $options['object_manager'];
  47.             if (!$em instanceof ObjectManager) {
  48.                 throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
  49.             }
  50.             // Use the custom manager instead.
  51.             $this->em $em;
  52.         } else if (isset($this->config['object_manager'])) {
  53.             $em $this->registry->getManager($this->config['object_manager']);
  54.             if (!$em instanceof ObjectManager) {
  55.                 throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
  56.             }
  57.             $this->em $em;
  58.         }
  59.         else {
  60.             $manager $this->registry->getManagerForClass($options['class']);
  61.             if ($manager instanceof ObjectManager) {
  62.                 $this->em $manager;
  63.             }
  64.         }
  65.         // add custom data transformer
  66.         if ($options['transformer']) {
  67.             if (!is_string($options['transformer'])) {
  68.                 throw new \Exception('The option transformer must be a string');
  69.             }
  70.             if (!class_exists($options['transformer'])) {
  71.                 throw new \Exception('Unable to load class: '.$options['transformer']);
  72.             }
  73.             $transformer = new $options['transformer']($this->em$options['class'], $options['text_property'], $options['primary_key']);
  74.             if (!$transformer instanceof DataTransformerInterface) {
  75.                 throw new \Exception(sprintf('The custom transformer %s must implement "Symfony\Component\Form\DataTransformerInterface"'get_class($transformer)));
  76.             }
  77.             // add the default data transformer
  78.         } else {
  79.             $newTagPrefix $options['allow_add']['new_tag_prefix'] ?? $this->config['allow_add']['new_tag_prefix'];
  80.             $newTagText $options['allow_add']['new_tag_text'] ?? $this->config['allow_add']['new_tag_text'];
  81.             $transformer $options['multiple']
  82.                 ? new EntitiesToPropertyTransformer($this->em$options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix$newTagText)
  83.                 : new EntityToPropertyTransformer($this->em$options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix$newTagText);
  84.         }
  85.         $builder->addViewTransformer($transformertrue);
  86.     }
  87.     public function finishView(FormView $viewFormInterface $form, array $options)
  88.     {
  89.         parent::finishView($view$form$options);
  90.         // make variables available to the view
  91.         $view->vars['remote_path'] = $options['remote_path']
  92.             ?: $this->router->generate($options['remote_route'], array_merge($options['remote_params'], ['page_limit' => $options['page_limit'] ]));
  93.         // merge variable names which are only set per instance with those from yml config
  94.         $varNames array_merge(['multiple''placeholder''primary_key''autostart''query_parameters'], array_keys($this->config));
  95.         foreach ($varNames as $varName) {
  96.             $view->vars[$varName] = $options[$varName];
  97.         }
  98.         if (isset($options['req_params']) && is_array($options['req_params']) && count($options['req_params']) > 0) {
  99.             $accessor PropertyAccess::createPropertyAccessor();
  100.             $reqParams = [];
  101.             foreach ($options['req_params'] as $key => $reqParam) {
  102.                 $reqParams[$key] = $accessor->getValue($view,  $reqParam '.vars[full_name]');
  103.             }
  104.             $view->vars['attr']['data-req_params'] = json_encode($reqParams);
  105.         }
  106.         //tags options
  107.         $varNames array_keys($this->config['allow_add']);
  108.         foreach ($varNames as $varName) {
  109.             $view->vars['allow_add'][$varName] = $options['allow_add'][$varName] ?? $this->config['allow_add'][$varName];
  110.         }
  111.         if ($options['multiple']) {
  112.             $view->vars['full_name'] .= '[]';
  113.         }
  114.         $view->vars['class_type'] = $options['class_type'];
  115.     }
  116.     /**
  117.      * @param OptionsResolver $resolver
  118.      */
  119.     public function configureOptions(OptionsResolver $resolver)
  120.     {
  121.         $resolver->setDefaults([
  122.                 'object_manager' => null,
  123.                 'class' => null,
  124.                 'data_class' => null,
  125.                 'primary_key' => 'id',
  126.                 'remote_path' => null,
  127.                 'remote_route' => null,
  128.                 'remote_params' => [],
  129.                 'multiple' => false,
  130.                 'compound' => false,
  131.                 'minimum_input_length' => $this->config['minimum_input_length'],
  132.                 'page_limit' => $this->config['page_limit'],
  133.                 'scroll' => $this->config['scroll'],
  134.                 'allow_clear' => $this->config['allow_clear'],
  135.                 'allow_add' => [
  136.                     'enabled' => $this->config['allow_add']['enabled'],
  137.                     'new_tag_text' => $this->config['allow_add']['new_tag_text'],
  138.                     'new_tag_prefix' => $this->config['allow_add']['new_tag_prefix'],
  139.                     'tag_separators' => $this->config['allow_add']['tag_separators']
  140.                 ],
  141.                 'delay' => $this->config['delay'],
  142.                 'text_property' => null,
  143.                 'placeholder' => false,
  144.                 'language' => $this->config['language'],
  145.         'theme' => $this->config['theme'],
  146.                 'required' => false,
  147.                 'cache' => $this->config['cache'],
  148.                 'cache_timeout' => $this->config['cache_timeout'],
  149.                 'transformer' => null,
  150.                 'autostart' => true,
  151.                 'width' => $this->config['width'] ?? null,
  152.                 'req_params' => [],
  153.                 'property' => null,
  154.                 'callback' => null,
  155.                 'class_type' => null,
  156.                 'query_parameters' => [],
  157.                 'render_html' => $this->config['render_html'] ?? false
  158.             ]
  159.         );
  160.     }
  161.     /**
  162.      * @return string
  163.      */
  164.     public function getBlockPrefix()
  165.     {
  166.         return 'tetranz_select2entity';
  167.     }
  168. }