1 | <?php |
||
30 | class FormContractor implements FormContractorInterface |
||
31 | { |
||
32 | /** |
||
33 | * @deprecated since version 3.1.0, to be removed in 4.0 |
||
34 | * |
||
35 | * @var FormFactoryInterface |
||
36 | */ |
||
37 | protected $fieldFactory; |
||
38 | |||
39 | /** |
||
40 | * @var FormFactoryInterface |
||
41 | */ |
||
42 | private $formFactory; |
||
43 | |||
44 | public function __construct(FormFactoryInterface $formFactory) |
||
48 | |||
49 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
80 | |||
81 | /** |
||
82 | * @return FormFactoryInterface |
||
83 | */ |
||
84 | public function getFormFactory() |
||
88 | |||
89 | public function getFormBuilder($name, array $options = []) |
||
93 | |||
94 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
95 | { |
||
96 | $options = []; |
||
97 | $options['sonata_field_description'] = $fieldDescription; |
||
98 | |||
99 | if ($this->checkFormClass($type, [ |
||
100 | ModelType::class, |
||
101 | ModelTypeList::class, |
||
102 | ModelListType::class, |
||
103 | ModelHiddenType::class, |
||
104 | ModelAutocompleteType::class, |
||
105 | ])) { |
||
106 | if ('list' === $fieldDescription->getOption('edit')) { |
||
107 | throw new \LogicException( |
||
108 | 'The ``Sonata\AdminBundle\Form\Type\ModelType`` type does not accept an ``edit`` option anymore,' |
||
109 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | $options['class'] = $fieldDescription->getTargetModel(); |
||
|
|||
114 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
115 | |||
116 | if ($this->checkFormClass($type, [ModelAutocompleteType::class])) { |
||
117 | if (!$fieldDescription->getAssociationAdmin()) { |
||
118 | throw new \RuntimeException(sprintf( |
||
119 | 'The current field `%s` is not linked to an admin.' |
||
120 | .' Please create one for the target entity: `%s`', |
||
121 | $fieldDescription->getName(), |
||
122 | $fieldDescription->getTargetModel() |
||
123 | )); |
||
124 | } |
||
125 | } |
||
126 | } elseif ($this->checkFormClass($type, [AdminType::class])) { |
||
127 | if (!$fieldDescription->getAssociationAdmin()) { |
||
128 | throw new \RuntimeException(sprintf( |
||
129 | 'The current field `%s` is not linked to an admin.' |
||
130 | .' Please create one for the target entity : `%s`', |
||
131 | $fieldDescription->getName(), |
||
132 | $fieldDescription->getTargetModel() |
||
133 | )); |
||
134 | } |
||
135 | |||
136 | if (!\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) { |
||
137 | throw new \RuntimeException(sprintf( |
||
138 | 'You are trying to add `%s` field `%s` which is not One-To-One or Many-To-One.' |
||
139 | .' Maybe you want `%s` instead?', |
||
140 | AdminType::class, |
||
141 | $fieldDescription->getName(), |
||
142 | CollectionType::class |
||
143 | )); |
||
144 | } |
||
145 | |||
146 | // set sensitive default value to have a component working fine out of the box |
||
147 | $options['btn_add'] = false; |
||
148 | $options['delete'] = false; |
||
149 | |||
150 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
151 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
152 | } elseif ($this->checkFormClass($type, [ |
||
153 | CollectionType::class, |
||
154 | ])) { |
||
155 | if (!$fieldDescription->getAssociationAdmin()) { |
||
156 | throw new \RuntimeException(sprintf( |
||
157 | 'The current field `%s` is not linked to an admin.' |
||
158 | .' Please create one for the target entity : `%s`', |
||
159 | $fieldDescription->getName(), |
||
160 | $fieldDescription->getTargetModel() |
||
161 | )); |
||
162 | } |
||
163 | |||
164 | $options['type'] = AdminType::class; |
||
165 | $options['modifiable'] = true; |
||
166 | $options['type_options'] = [ |
||
167 | 'sonata_field_description' => $fieldDescription, |
||
168 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
169 | ]; |
||
170 | } |
||
171 | |||
172 | return $options; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $type |
||
177 | * @param array $classes |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | private function checkFormClass($type, $classes) |
||
187 | } |
||
188 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: