|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Braincrafted\Bundle\BootstrapBundle\Form\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
|
6
|
|
|
use Symfony\Component\Form\ButtonBuilder; |
|
7
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
8
|
|
|
use Symfony\Component\Form\FormInterface; |
|
9
|
|
|
use Symfony\Component\Form\FormView; |
|
10
|
|
|
|
|
11
|
|
|
use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class InputGroupButtonExtension |
|
15
|
|
|
* |
|
16
|
|
|
* Handles prepended and appended buttons to input fields. |
|
17
|
|
|
* Buttons are created and stored during the BuildForm phase and rendered during the buildView Phase. |
|
18
|
|
|
* |
|
19
|
|
|
* Known issues: |
|
20
|
|
|
* - since at build time the form parent is unavailable, two forms with fields of the same name, with buttons attached |
|
21
|
|
|
* may cause conflict. |
|
22
|
|
|
* |
|
23
|
|
|
* @package Braincrafted\Bundle\BootstrapBundle\Form\Extension |
|
24
|
|
|
*/ |
|
25
|
|
|
class InputGroupButtonExtension extends AbstractTypeExtension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $buttons = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the name of the type being extended. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string The name of the type being extended |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getExtendedType() |
|
38
|
|
|
{ |
|
39
|
|
|
// map old class to new one using LegacyFormHelper |
|
40
|
|
|
return LegacyFormHelper::getType('text'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
|
47
|
|
|
{ |
|
48
|
|
|
|
|
49
|
|
|
if (!isset($this->buttons[$form->getName()])) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$storedButtons = $this->buttons[$form->getName()]; |
|
54
|
|
|
|
|
55
|
|
|
if (isset($storedButtons['prepend']) && $storedButtons['prepend'] !== null) { |
|
56
|
|
|
$view->vars['input_group_button_prepend'] = $storedButtons['prepend']->getForm()->createView(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (isset($storedButtons['append']) && $storedButtons['append'] !== null) { |
|
60
|
|
|
$view->vars['input_group_button_append'] = $storedButtons['append']->getForm()->createView(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!isset($options['attr']) || !isset($options['attr']['input_group'])) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($options['attr']['input_group']['button_prepend'])) { |
|
74
|
|
|
$this->storeButton( |
|
75
|
|
|
$this->addButton( |
|
76
|
|
|
$builder, |
|
77
|
|
|
$options['attr']['input_group']['button_prepend'] |
|
78
|
|
|
), |
|
79
|
|
|
$builder, |
|
80
|
|
|
'prepend' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (isset($options['attr']['input_group']['button_append'])) { |
|
85
|
|
|
$this->storeButton( |
|
86
|
|
|
$this->addButton( |
|
87
|
|
|
$builder, |
|
88
|
|
|
$options['attr']['input_group']['button_append'] |
|
89
|
|
|
), |
|
90
|
|
|
$builder, |
|
91
|
|
|
'append' |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Adds a button |
|
98
|
|
|
* |
|
99
|
|
|
* @param FormBuilderInterface $builder |
|
100
|
|
|
* @param array $config |
|
101
|
|
|
* |
|
102
|
|
|
* @return ButtonBuilder |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function addButton(FormBuilderInterface $builder, $config) |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
$options = (isset($config['options']))? $config['options'] : array(); |
|
108
|
|
|
|
|
109
|
|
|
return $builder->create($config['name'], $config['type'], $options); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Stores a button for later rendering |
|
114
|
|
|
* |
|
115
|
|
|
* @param ButtonBuilder $buttonBuilder |
|
116
|
|
|
* @param FormBuilderInterface $form |
|
117
|
|
|
* @param string $position |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function storeButton(ButtonBuilder $buttonBuilder, FormBuilderInterface $form, $position) |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
|
|
if (!isset($this->buttons[$form->getName()])) { |
|
123
|
|
|
$this->buttons[$form->getName()] = array(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->buttons[$form->getName()][$position] = $buttonBuilder; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|