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: * Saisie d'une valeur textuelle (avec auto-complétion).
13: * Intégration du composant JS twitter typeahead
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: * {@inheritdoc}
26: */
27: public function configureOptions(OptionsResolver $resolver):void {
28: parent::configureOptions($resolver);
29:
30: $resolver->setDefaults(array(
31: 'compound' => false,
32: 'choices' => array(), // liste des choix à l'initialisation du widget
33: 'prefetch' => null, // nom de route pour la requête Ajax prefetch de Bloodhound
34: 'remote' => null, // nom de route pour la requête Ajax remote de Bloodhound
35: 'bloodhound' => false, // utilisation du moteur de suggestion Bloodhound (auto si prefetch/remote)
36: 'minLength' => 1, // nombre de caractères à saisir avant affichage des suggestions
37: 'maxDisplay' => 5, // nombre maximum de suggestions affichées en dropdown
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: * {@inheritdoc}
55: */
56: public function buildView(FormView $view, FormInterface $form, array $options):void {
57: parent::buildView($view, $form, $options);
58:
59: // spécification de la taille du widget (ajout de classe bootstrap)
60: $this->setViewInputSizing($view);
61:
62: // les recherches ajax (prefetch / remote) requiert le moteur bloodhound
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: