| 1: | <?php |
| 2: | namespace TIC\FormBundle\Form\Type; |
| 3: | |
| 4: | |
| 5: | use TIC\FormBundle\Base\TICWidgetType as BaseType; |
| 6: | |
| 7: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 8: | use Symfony\Component\OptionsResolver\Options; |
| 9: | use Symfony\Component\Form\FormBuilderInterface; |
| 10: | use Symfony\Component\Form\FormInterface; |
| 11: | use Symfony\Component\Form\FormView; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class EntityType extends BaseType { |
| 18: | |
| 19: | protected $form_alias = 'tic_entity'; |
| 20: | protected $form_parent = 'Symfony\Bridge\Doctrine\Form\Type\EntityType'; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function configureOptions(OptionsResolver $resolver): void { |
| 27: | parent::configureOptions($resolver); |
| 28: | |
| 29: | $resolver->setDefaults(array( |
| 30: | 'with_js' => true, |
| 31: | 'plugins' => array(), |
| 32: | 'remove' => true, |
| 33: | )); |
| 34: | |
| 35: | $resolver->setAllowedTypes('with_js', array('bool')); |
| 36: | $resolver->setAllowedTypes('plugins', array('array')); |
| 37: | $resolver->setAllowedTypes('remove', array('bool')); |
| 38: | |
| 39: | $resolver->setAllowedValues('expanded', array(false, true, 'inline')); |
| 40: | |
| 41: | $resolver->setNormalizer('expanded', function (Options $options, $expanded) { |
| 42: | if ($expanded && $options['with_js']) { |
| 43: | @trigger_error("JS Selectize don't have an expanded view"); |
| 44: | return false; |
| 45: | } |
| 46: | return $expanded; |
| 47: | }); |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function buildView(FormView $view, FormInterface $form, array $options):void { |
| 55: | parent::buildView($view, $form, $options); |
| 56: | |
| 57: | |
| 58: | $this->setViewInputSizing($view); |
| 59: | |
| 60: | if ($options['multiple'] && $options['remove']) array_push($options['plugins'], 'remove_button'); |
| 61: | $view->vars['plugins'] = array_unique($options['plugins']); |
| 62: | $view->vars['with_js'] = empty($options['with_js']) ? false : true; |
| 63: | |
| 64: | if ($options['expanded'] === 'inline') { |
| 65: | $class = $options['multiple'] ? 'checkbox-inline' : 'radio-inline'; |
| 66: | $view->vars['label_attr'] = $options['label_attr'] + array('class' => ''); |
| 67: | $view->vars['label_attr']['class'] = trim($view->vars['label_attr']['class'].' '.$class); |
| 68: | $view->vars['expanded'] = true; |
| 69: | } |
| 70: | } |
| 71: | |
| 72: | |
| 73: | } |
| 74: | |