This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
FormContractor.php
198 lines (171 loc) · 7.18 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
<?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\DoctrinePHPCRAdminBundle\Builder;
use Doctrine\ODM\PHPCR\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\ModelType;
use Sonata\AdminBundle\Form\Type\ModelTypeList;
use Sonata\CoreBundle\Form\Type\CollectionType as DeprecatedCollectionType;
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType;
use Sonata\Form\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
class FormContractor implements FormContractorInterface
{
/**
* @var FormFactoryInterface
*/
protected $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
/**
* The method defines the correct default settings for the provided FieldDescription.
*
* {@inheritdoc}
*
* @throws \RuntimeException if the $fieldDescription does not specify a type
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
$metadata = null;
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
/** @var ClassMetadata $metadata */
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->mappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
}
// set the default association mapping
if ($metadata->hasAssociation($fieldDescription->getName())) {
$fieldDescription->setAssociationMapping($metadata->getAssociation($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'));
$mappingTypes = [
ClassMetadata::MANY_TO_ONE,
ClassMetadata::MANY_TO_MANY,
'children',
'child',
'parent',
'referrers',
];
if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && \in_array($fieldDescription->getMappingType(), $mappingTypes, true)) {
$admin->attachAdminClass($fieldDescription);
}
}
/**
* @return FormFactoryInterface
*/
public function getFormFactory()
{
return $this->formFactory;
}
/**
* {@inheritdoc}
*/
public function getFormBuilder($name, array $options = [])
{
return $this->getFormFactory()->createNamedBuilder(
$name,
FormType::class,
null,
$options
);
}
/**
* {@inheritdoc}
*
* @throws \LogicException if a sonata_type_model field does not have a
* target model configured
*/
public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
{
$options = [];
$options['sonata_field_description'] = $fieldDescription;
switch ($type) {
case TreeModelType::class:
case 'doctrine_phpcr_odm_tree':
$options['class'] = $fieldDescription->getTargetEntity();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
break;
case ModelType::class:
case 'sonata_type_model':
case ModelTypeList::class:
case 'sonata_type_model_list':
if ('child' !== $fieldDescription->getMappingType() && !$fieldDescription->getTargetEntity()) {
throw new \LogicException(sprintf(
'The field "%s" in class "%s" does not have a target model defined. Please specify the "targetDocument" attribute in the mapping for this class.',
$fieldDescription->getName(),
$fieldDescription->getAdmin()->getClass()
));
}
$options['class'] = $fieldDescription->getTargetEntity();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
break;
case AdminType::class:
case 'sonata_type_admin':
if (!$fieldDescription->getAssociationAdmin()) {
throw $this->getAssociationAdminException($fieldDescription);
}
$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
break;
case DeprecatedCollectionType::class:
case 'sonata_type_collection_legacy':
/*
* NEXT_MAJOR: Remove 'Sonata\CoreBundle\Form\Type\CollectionType' and 'sonata_type_collection_legacy'
* cases when replace SonataCoreBundle by SonataFormExtension
*/
case CollectionType::class:
case 'sonata_type_collection':
if (!$fieldDescription->getAssociationAdmin()) {
throw $this->getAssociationAdminException($fieldDescription);
}
$options['type'] = AdminType::class;
$options['modifiable'] = true;
$options['type_options'] = [
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
];
break;
}
return $options;
}
/**
* @return \LogicException
*/
protected function getAssociationAdminException(FieldDescriptionInterface $fieldDescription)
{
$msg = sprintf('The current field `%s` is not linked to an admin. Please create one', $fieldDescription->getName());
if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::MANY_TO_ONE, ClassMetadata::MANY_TO_MANY, 'referrers'], true)) {
if ($fieldDescription->getTargetEntity()) {
$msg .= " for the target document: `{$fieldDescription->getTargetEntity()}`";
}
$msg .= ', specify the `targetDocument` in the Reference, or the `referringDocument` in the Referrers or use the option `admin_code` to link it.';
} else {
$msg .= ' and use the option `admin_code` to link it.';
}
return new \LogicException($msg);
}
}