/
FormContractor.php
208 lines (182 loc) · 7.72 KB
/
FormContractor.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrineORMAdminBundle\Builder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\FormContractorInterface;
use Sonata\AdminBundle\Form\Type\AdminType;
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
use Sonata\AdminBundle\Form\Type\ModelHiddenType;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Form\Type\ModelTypeList;
use Sonata\CoreBundle\Form\Type\CollectionType as DeprecatedCollectionType;
use Sonata\Form\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
class FormContractor implements FormContractorInterface
{
/**
* NEXT_MAJOR: remove this property.
*
* @deprecated since version 3.0.4, to be removed in 4.0
*
* @var FormFactoryInterface
*/
protected $fieldFactory;
/**
* @var FormFactoryInterface
*/
protected $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
}
// set the default association mapping
if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
}
}
if (!$fieldDescription->getType()) {
throw new \RuntimeException(sprintf(
'Please define a type for field `%s` in `%s`',
$fieldDescription->getName(),
\get_class($admin)
));
}
$fieldDescription->setAdmin($admin);
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
if ($this->hasAssociation($fieldDescription) || $fieldDescription->getOption('admin_code')) {
$admin->attachAdminClass($fieldDescription);
}
}
/**
* @return FormFactoryInterface
*/
public function getFormFactory()
{
return $this->formFactory;
}
public function getFormBuilder($name, array $options = [])
{
return $this->getFormFactory()->createNamedBuilder($name, FormType::class, null, $options);
}
public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
{
$options = [];
$options['sonata_field_description'] = $fieldDescription;
if ($this->checkFormClass($type, [
ModelType::class,
ModelTypeList::class,
ModelListType::class,
ModelHiddenType::class,
ModelAutocompleteType::class,
])) {
if ('list' === $fieldDescription->getOption('edit')) {
throw new \LogicException(
'The `\Sonata\AdminBundle\Form\Type\ModelType` type does not accept an `edit` option anymore,'
.' please review the UPGRADE-2.1.md file from the SonataAdminBundle'
);
}
$options['class'] = $fieldDescription->getTargetEntity();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
if ($this->checkFormClass($type, [ModelAutocompleteType::class])) {
if (!$fieldDescription->getAssociationAdmin()) {
throw new \RuntimeException(sprintf(
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity: `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
));
}
}
} elseif ($this->checkFormClass($type, [AdminType::class])) {
if (!$fieldDescription->getAssociationAdmin()) {
throw new \RuntimeException(sprintf(
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity : `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
));
}
if (!\in_array($fieldDescription->getMappingType(), [
ClassMetadata::ONE_TO_ONE,
ClassMetadata::MANY_TO_ONE,
], true)) {
throw new \RuntimeException(sprintf(
'You are trying to add `Sonata\AdminBundle\Form\Type\AdminType` field `%s` which is not One-To-One or Many-To-One.'
.' Maybe you want `Sonata\Form\Type\CollectionType` instead?',
$fieldDescription->getName()
));
}
// set sensitive default value to have a component working fine out of the box
$options['btn_add'] = false;
$options['delete'] = false;
$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
$options['empty_data'] = static function () use ($fieldDescription) {
return $fieldDescription->getAssociationAdmin()->getNewInstance();
};
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
} elseif ($this->checkFormClass($type, [CollectionType::class, DeprecatedCollectionType::class])) {
if (!$fieldDescription->getAssociationAdmin()) {
throw new \RuntimeException(sprintf(
'The current field `%s` is not linked to an admin.'
.' Please create one for the target entity : `%s`',
$fieldDescription->getName(),
$fieldDescription->getTargetEntity()
));
}
$options['type'] = AdminType::class;
$options['modifiable'] = true;
$options['type_options'] = [
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
'empty_data' => static function () use ($fieldDescription) {
return $fieldDescription->getAssociationAdmin()->getNewInstance();
},
];
}
return $options;
}
/**
* @return bool
*/
private function hasAssociation(FieldDescriptionInterface $fieldDescription)
{
return \in_array($fieldDescription->getMappingType(), [
ClassMetadata::ONE_TO_MANY,
ClassMetadata::MANY_TO_MANY,
ClassMetadata::MANY_TO_ONE,
ClassMetadata::ONE_TO_ONE,
], true);
}
/**
* @param string $type
* @param array $classes
*
* @return array
*/
private function checkFormClass($type, $classes)
{
return array_filter($classes, static function ($subclass) use ($type) {
return is_a($type, $subclass, true);
});
}
}