1 | <?php |
||
20 | class FormContractor implements FormContractorInterface |
||
21 | { |
||
22 | /** |
||
23 | * NEXT_MAJOR: remove this property. |
||
24 | * |
||
25 | * @deprecated since version 3.0.4, to be removed in 4.0 |
||
26 | * |
||
27 | * @var FormFactoryInterface |
||
28 | */ |
||
29 | protected $fieldFactory; |
||
30 | |||
31 | /** |
||
32 | * @var FormFactoryInterface |
||
33 | */ |
||
34 | protected $formFactory; |
||
35 | |||
36 | /** |
||
37 | * @param FormFactoryInterface $formFactory |
||
38 | */ |
||
39 | public function __construct(FormFactoryInterface $formFactory) |
||
40 | { |
||
41 | $this->formFactory = $formFactory; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
48 | { |
||
49 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
50 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
51 | |||
52 | // set the default field mapping |
||
53 | if (isset($metadata->fieldMappings[$fieldDescription->getName()])) { |
||
54 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]); |
||
55 | } |
||
56 | |||
57 | // set the default association mapping |
||
58 | if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
||
59 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | if (!$fieldDescription->getType()) { |
||
64 | throw new \RuntimeException(sprintf( |
||
65 | 'Please define a type for field `%s` in `%s`', |
||
66 | $fieldDescription->getName(), |
||
67 | get_class($admin) |
||
68 | )); |
||
69 | } |
||
70 | |||
71 | $fieldDescription->setAdmin($admin); |
||
72 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard')); |
||
73 | |||
74 | if ($this->hasAssociation($fieldDescription) || $fieldDescription->getOption('admin_code')) { |
||
75 | $admin->attachAdminClass($fieldDescription); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return FormFactoryInterface |
||
81 | */ |
||
82 | public function getFormFactory() |
||
83 | { |
||
84 | return $this->formFactory; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function getFormBuilder($name, array $options = []) |
||
91 | { |
||
92 | return $this->getFormFactory()->createNamedBuilder( |
||
93 | $name, |
||
94 | 'Symfony\Component\Form\Extension\Core\Type\FormType', |
||
95 | null, |
||
96 | $options |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
104 | { |
||
105 | $options = []; |
||
106 | $options['sonata_field_description'] = $fieldDescription; |
||
107 | if ($this->checkFormClass($type, [ |
||
108 | 'Sonata\AdminBundle\Form\Type\ModelType', |
||
109 | 'Sonata\AdminBundle\Form\Type\ModelTypeList', |
||
110 | 'Sonata\AdminBundle\Form\Type\ModelListType', |
||
111 | 'Sonata\AdminBundle\Form\Type\ModelHiddenType', |
||
112 | 'Sonata\AdminBundle\Form\Type\ModelAutocompleteType', |
||
113 | ])) { |
||
114 | if ($fieldDescription->getOption('edit') === 'list') { |
||
115 | throw new \LogicException( |
||
116 | 'The `sonata_type_model` type does not accept an `edit` option anymore,' |
||
117 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
122 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
123 | |||
124 | if ($this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\ModelAutocompleteType'])) { |
||
125 | if (!$fieldDescription->getAssociationAdmin()) { |
||
126 | throw new \RuntimeException(sprintf( |
||
127 | 'The current field `%s` is not linked to an admin.' |
||
128 | .' Please create one for the target entity: `%s`', |
||
129 | $fieldDescription->getName(), |
||
130 | $fieldDescription->getTargetEntity() |
||
131 | )); |
||
132 | } |
||
133 | } |
||
134 | <<<<<<< HEAD |
||
135 | } elseif ($this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\AdminType'])) { |
||
136 | ======= |
||
137 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8 |
||
138 | } elseif ($this->checkFormType($type, ['sonata_type_admin']) || $this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\AdminType'])) { |
||
139 | >>>>>>> origin/3.x |
||
140 | if (!$fieldDescription->getAssociationAdmin()) { |
||
141 | throw new \RuntimeException(sprintf( |
||
142 | 'The current field `%s` is not linked to an admin.' |
||
143 | .' Please create one for the target entity : `%s`', |
||
144 | $fieldDescription->getName(), |
||
145 | $fieldDescription->getTargetEntity() |
||
146 | )); |
||
147 | } |
||
148 | |||
149 | if (!in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE])) { |
||
150 | throw new \RuntimeException(sprintf( |
||
151 | 'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One.' |
||
152 | .' Maybe you want `sonata_type_collection` instead?', |
||
153 | $fieldDescription->getName() |
||
154 | )); |
||
155 | } |
||
156 | |||
157 | // set sensitive default value to have a component working fine out of the box |
||
158 | $options['btn_add'] = false; |
||
159 | $options['delete'] = false; |
||
160 | |||
161 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
162 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
163 | <<<<<<< HEAD |
||
164 | } elseif ($this->checkFormClass($type, ['Sonata\CoreBundle\Form\Type\CollectionType'])) { |
||
165 | ======= |
||
166 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8 |
||
167 | } elseif ($this->checkFormType($type, ['sonata_type_collection']) || $this->checkFormClass($type, ['Sonata\CoreBundle\Form\Type\CollectionType'])) { |
||
168 | >>>>>>> origin/3.x |
||
169 | if (!$fieldDescription->getAssociationAdmin()) { |
||
170 | throw new \RuntimeException(sprintf( |
||
171 | 'The current field `%s` is not linked to an admin.' |
||
172 | .' Please create one for the target entity : `%s`', |
||
173 | $fieldDescription->getName(), |
||
174 | $fieldDescription->getTargetEntity() |
||
175 | )); |
||
176 | } |
||
177 | |||
178 | $options['type'] = 'sonata_type_admin'; |
||
179 | $options['modifiable'] = true; |
||
180 | $options['type_options'] = [ |
||
181 | 'sonata_field_description' => $fieldDescription, |
||
182 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
183 | ]; |
||
184 | } |
||
185 | |||
186 | return $options; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param FieldDescriptionInterface $fieldDescription |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | private function hasAssociation(FieldDescriptionInterface $fieldDescription) |
||
195 | { |
||
196 | return in_array($fieldDescription->getMappingType(), [ |
||
197 | ClassMetadataInfo::ONE_TO_MANY, |
||
198 | ClassMetadataInfo::MANY_TO_MANY, |
||
199 | ClassMetadataInfo::MANY_TO_ONE, |
||
200 | ClassMetadataInfo::ONE_TO_ONE, |
||
201 | ]); |
||
202 | <<<<<<< HEAD |
||
203 | ======= |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * NEXT_MAJOR: See next major comments above, this method should be removed when dropping support for Symfony <2.8. |
||
208 | * |
||
233 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.