/
AutocompleterType.php
135 lines (116 loc) · 3.67 KB
/
AutocompleterType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* This file is part of the GenemuFormBundle package.
*
* (c) Olivier Chauvel <olivier@generation-multiple.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Genemu\Bundle\FormBundle\Form\JQuery\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Genemu\Bundle\FormBundle\Form\Core\ChoiceList\AjaxSimpleChoiceList;
use Genemu\Bundle\FormBundle\Form\Core\DataTransformer\ChoiceToJsonTransformer;
/**
* @author Olivier Chauvel <olivier@generation-multiple.com>
*/
class AutocompleterType extends AbstractType
{
private $widget;
public function __construct($widget)
{
$this->widget = $widget;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new ChoiceToJsonTransformer(
$options['choice_list'],
$options['ajax'],
$this->widget,
$options['multiple']
));
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$datas = json_decode($form->getViewData(), true);
$value = '';
if (!empty($datas)) {
if ($options['multiple']) {
foreach ($datas as $data) {
$value .= $data['label'] . ', ';
}
} else {
$value = $datas['label'];
}
}
$view->vars = array_replace($view->vars, array(
'autocompleter_value' => $value,
'route_name' => $options['route_name'],
'free_values' => $options['free_values'],
));
// Adds a custom block prefix
array_splice(
$view->vars['block_prefixes'],
array_search($this->getName(), $view->vars['block_prefixes']),
0,
'genemu_jqueryautocompleter'
);
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$widget = $this->widget;
$resolver->setDefaults(array(
'route_name' => null,
'ajax' => function (Options $options, $previousValue) {
if (!empty($options['route_name']) || $options['free_values']) {
return true;
}
return false;
},
'choice_list' => function (Options $options, $previousValue) use ($widget) {
if (!in_array($widget, array('entity', 'document', 'model'))) {
return new AjaxSimpleChoiceList($options['choices'], $options['ajax']);
}
return $previousValue;
},
'freeValues' => false,
'free_values' => function (Options $options, $previousValue) {
if ($options['multiple']) {
return false;
}
return $options['freeValues'] ?: $previousValue;
}
));
}
/**
* {@inheritdoc}
*/
public function getParent()
{
if (in_array($this->widget, array('entity', 'document', 'model'), true)) {
return 'genemu_ajax' . $this->widget;
}
return $this->widget;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'genemu_jqueryautocompleter_' . $this->widget;
}
}