/
WidgetCollectionFormTypeExtension.php
114 lines (101 loc) · 4.01 KB
/
WidgetCollectionFormTypeExtension.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
<?php
/*
* This file is part of the MopaBootstrapBundle.
*
* (c) Philipp A. Mohrenweiser <phiamo@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mopa\Bundle\BootstrapBundle\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Extension for Form collections.
*
* @author phiamo <phiamo@googlemail.com>
*/
class WidgetCollectionFormTypeExtension extends AbstractTypeExtension
{
/**
* @var array
*/
protected $options;
/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options)
{
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (in_array('collection', $view->vars['block_prefixes'])) {
if ($options['widget_add_btn'] !== null && !is_array($options['widget_add_btn'])) {
throw new InvalidArgumentException('The "widget_add_btn" option must be an "array".');
}
if ((isset($options['allow_add']) && true === $options['allow_add']) && $options['widget_add_btn']) {
if (isset($options['widget_add_btn']['attr']) && !is_array($options['widget_add_btn']['attr'])) {
throw new InvalidArgumentException('The "widget_add_btn.attr" option must be an "array".');
}
$options['widget_add_btn'] = array_replace_recursive($this->options['widget_add_btn'], $options['widget_add_btn']);
}
}
if ($view->parent && in_array('collection', $view->parent->vars['block_prefixes'])) {
if ($options['widget_remove_btn'] !== null && !is_array($options['widget_remove_btn'])) {
throw new InvalidArgumentException('The "widget_remove_btn" option must be an "array".');
}
if ((isset($view->parent->vars['allow_delete']) && true === $view->parent->vars['allow_delete']) && $options['widget_remove_btn']) {
if (isset($options['widget_remove_btn']) && !is_array($options['widget_remove_btn'])) {
throw new InvalidArgumentException('The "widget_remove_btn" option must be an "array".');
}
$options['widget_remove_btn'] = array_replace_recursive($this->options['widget_remove_btn'], $options['widget_remove_btn']);
}
}
$view->vars['omit_collection_item'] = $options['omit_collection_item'];
$view->vars['widget_add_btn'] = $options['widget_add_btn'];
$view->vars['widget_remove_btn'] = $options['widget_remove_btn'];
$view->vars['prototype_names'] = $options['prototype_names'];
}
/**
* {@inheritdoc}
*
* @deprecated Remove it when bumping requirements to SF 2.7+
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'omit_collection_item' => true === $this->options['render_collection_item'] ? false : true,
'widget_add_btn' => $this->options['widget_add_btn'],
'widget_remove_btn' => $this->options['widget_remove_btn'],
'prototype_names' => array(),
));
}
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
? 'Symfony\Component\Form\Extension\Core\Type\FormType'
: 'form' // SF <2.8 BC
;
}
}