| 1: | <?php |
| 2: | namespace TIC\FormBundle\Form\Type; |
| 3: | |
| 4: | use TIC\FormBundle\Base\TICWidgetType as BaseType; |
| 5: | |
| 6: | use Symfony\Component\Form\FormInterface; |
| 7: | use Symfony\Component\Form\FormView; |
| 8: | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 9: | use Symfony\Component\OptionsResolver\Options; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | class TypeAheadType extends BaseType { |
| 16: | |
| 17: | protected $form_alias = 'tic_typeahead'; |
| 18: | protected $form_parent = null; |
| 19: | |
| 20: | protected $default_prefix = '<tag>'; |
| 21: | protected $default_suffix = '<tag>'; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | public function configureOptions(OptionsResolver $resolver):void { |
| 28: | parent::configureOptions($resolver); |
| 29: | |
| 30: | $resolver->setDefaults(array( |
| 31: | 'compound' => false, |
| 32: | 'choices' => array(), |
| 33: | 'prefetch' => null, |
| 34: | 'remote' => null, |
| 35: | 'bloodhound' => false, |
| 36: | 'minLength' => 1, |
| 37: | 'maxDisplay' => 5, |
| 38: | )); |
| 39: | |
| 40: | $resolver->setAllowedTypes('choices', array('array', 'callable')); |
| 41: | $resolver->setAllowedTypes('prefetch', array('null', 'string')); |
| 42: | $resolver->setAllowedTypes('remote', array('null', 'string')); |
| 43: | $resolver->setAllowedTypes('bloodhound', array('bool')); |
| 44: | $resolver->setAllowedTypes('minLength', array('int')); |
| 45: | $resolver->setAllowedTypes('maxDisplay', array('int', 'bool')); |
| 46: | |
| 47: | $resolver->setNormalizer('choices', function (Options $options, $choices) { |
| 48: | return is_callable($choices) ? call_user_func($choices, $options) : $choices; |
| 49: | }); |
| 50: | } |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function buildView(FormView $view, FormInterface $form, array $options):void { |
| 57: | parent::buildView($view, $form, $options); |
| 58: | |
| 59: | |
| 60: | $this->setViewInputSizing($view); |
| 61: | |
| 62: | |
| 63: | if (! empty($options['prefetch'])) $options['bloodhound'] = true; |
| 64: | if (! empty($options['remote'])) $options['bloodhound'] = true; |
| 65: | |
| 66: | $view->vars['bloodhound'] = $options['bloodhound']; |
| 67: | $view->vars['choices'] = $options['choices']; |
| 68: | $view->vars['prefetch'] = $options['prefetch']; |
| 69: | $view->vars['remote'] = $options['remote']; |
| 70: | $view->vars['minLength'] = $options['minLength']; |
| 71: | $view->vars['maxDisplay'] = $options['maxDisplay']; |
| 72: | } |
| 73: | |
| 74: | |
| 75: | } |
| 76: | |