]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Form\Extension\Core\Type; | |
13 | ||
14 | use Symfony\Component\Form\AbstractType; | |
15 | use Symfony\Component\Form\FormBuilderInterface; | |
16 | use Symfony\Component\Form\FormView; | |
17 | use Symfony\Component\Form\FormInterface; | |
18 | use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener; | |
19 | use Symfony\Component\OptionsResolver\Options; | |
20 | use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
21 | ||
22 | class CollectionType extends AbstractType | |
23 | { | |
24 | /** | |
25 | * {@inheritdoc} | |
26 | */ | |
27 | public function buildForm(FormBuilderInterface $builder, array $options) | |
28 | { | |
29 | if ($options['allow_add'] && $options['prototype']) { | |
30 | $prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array( | |
31 | 'label' => $options['prototype_name'].'label__', | |
32 | ), $options['options'])); | |
33 | $builder->setAttribute('prototype', $prototype->getForm()); | |
34 | } | |
35 | ||
36 | $resizeListener = new ResizeFormListener( | |
37 | $options['type'], | |
38 | $options['options'], | |
39 | $options['allow_add'], | |
40 | $options['allow_delete'] | |
41 | ); | |
42 | ||
43 | $builder->addEventSubscriber($resizeListener); | |
44 | } | |
45 | ||
46 | /** | |
47 | * {@inheritdoc} | |
48 | */ | |
49 | public function buildView(FormView $view, FormInterface $form, array $options) | |
50 | { | |
51 | $view->vars = array_replace($view->vars, array( | |
52 | 'allow_add' => $options['allow_add'], | |
53 | 'allow_delete' => $options['allow_delete'], | |
54 | )); | |
55 | ||
56 | if ($form->getConfig()->hasAttribute('prototype')) { | |
57 | $view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view); | |
58 | } | |
59 | } | |
60 | ||
61 | /** | |
62 | * {@inheritdoc} | |
63 | */ | |
64 | public function finishView(FormView $view, FormInterface $form, array $options) | |
65 | { | |
66 | if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) { | |
67 | $view->vars['multipart'] = true; | |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
72 | * {@inheritdoc} | |
73 | */ | |
74 | public function setDefaultOptions(OptionsResolverInterface $resolver) | |
75 | { | |
76 | $optionsNormalizer = function (Options $options, $value) { | |
77 | $value['block_name'] = 'entry'; | |
78 | ||
79 | return $value; | |
80 | }; | |
81 | ||
82 | $resolver->setDefaults(array( | |
83 | 'allow_add' => false, | |
84 | 'allow_delete' => false, | |
85 | 'prototype' => true, | |
86 | 'prototype_name' => '__name__', | |
87 | 'type' => 'text', | |
88 | 'options' => array(), | |
89 | )); | |
90 | ||
91 | $resolver->setNormalizers(array( | |
92 | 'options' => $optionsNormalizer, | |
93 | )); | |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritdoc} | |
98 | */ | |
99 | public function getName() | |
100 | { | |
101 | return 'collection'; | |
102 | } | |
103 | } |