|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sherlockode\SonataAdvancedContentBundle\Admin; |
| 4 | + |
| 5 | +use Sonata\AdminBundle\Builder\FormContractorInterface; |
| 6 | +use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface; |
| 7 | +use Sonata\AdminBundle\Form\Type\AdminType; |
| 8 | +use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
| 9 | +use Sonata\AdminBundle\Form\Type\ModelHiddenType; |
| 10 | +use Sonata\AdminBundle\Form\Type\ModelListType; |
| 11 | +use Sonata\AdminBundle\Form\Type\ModelReferenceType; |
| 12 | +use Sonata\AdminBundle\Form\Type\ModelType; |
| 13 | +use Sonata\Form\Type\CollectionType; |
| 14 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 15 | +use Symfony\Component\Form\FormBuilderInterface; |
| 16 | +use Symfony\Component\Form\FormFactoryInterface; |
| 17 | +use Symfony\Component\Form\FormRegistryInterface; |
| 18 | + |
| 19 | +class FormContractor implements FormContractorInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var FormFactoryInterface |
| 23 | + */ |
| 24 | + protected $formFactory; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var FormRegistryInterface |
| 28 | + */ |
| 29 | + protected $formRegistry; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $formType; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param FormFactoryInterface $formFactory |
| 38 | + * @param FormRegistryInterface $formRegistry |
| 39 | + */ |
| 40 | + public function __construct(FormFactoryInterface $formFactory, FormRegistryInterface $formRegistry) |
| 41 | + { |
| 42 | + $this->formFactory = $formFactory; |
| 43 | + $this->formRegistry = $formRegistry; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param FieldDescriptionInterface $fieldDescription |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function fixFieldDescription(FieldDescriptionInterface $fieldDescription): void |
| 52 | + { |
| 53 | + $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard')); |
| 54 | + |
| 55 | + if ($fieldDescription->describesAssociation() || null !== $fieldDescription->getOption('admin_code')) { |
| 56 | + $fieldDescription->getAdmin()->attachAdminClass($fieldDescription); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param string $name |
| 62 | + * @param array $formOptions |
| 63 | + * |
| 64 | + * @return FormBuilderInterface |
| 65 | + */ |
| 66 | + public function getFormBuilder(string $name, array $formOptions = []): FormBuilderInterface |
| 67 | + { |
| 68 | + return $this->formFactory->createNamedBuilder($name, $this->formType, null, $formOptions); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param string $formType |
| 73 | + * |
| 74 | + * @return $this |
| 75 | + */ |
| 76 | + public function setFormType(string $formType): self |
| 77 | + { |
| 78 | + $this->formType = $formType; |
| 79 | + |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param string|null $type |
| 85 | + * @param FieldDescriptionInterface $fieldDescription |
| 86 | + * @param array $formOptions |
| 87 | + * |
| 88 | + * @return array|mixed[] |
| 89 | + */ |
| 90 | + final public function getDefaultOptions( |
| 91 | + ?string $type, |
| 92 | + FieldDescriptionInterface $fieldDescription, |
| 93 | + array $formOptions = [] |
| 94 | + ): array { |
| 95 | + $options = []; |
| 96 | + $options['sonata_field_description'] = $fieldDescription; |
| 97 | + |
| 98 | + if ($this->isAnyInstanceOf($type, [ |
| 99 | + ModelType::class, |
| 100 | + ModelListType::class, |
| 101 | + ModelHiddenType::class, |
| 102 | + ModelAutocompleteType::class, |
| 103 | + ModelReferenceType::class, |
| 104 | + ])) { |
| 105 | + $options['class'] = $fieldDescription->getTargetModel(); |
| 106 | + $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
| 107 | + |
| 108 | + if ($this->isAnyInstanceOf($type, [ModelAutocompleteType::class])) { |
| 109 | + if (!$fieldDescription->hasAssociationAdmin()) { |
| 110 | + throw new \InvalidArgumentException(sprintf( |
| 111 | + 'The current field `%s` is not linked to an admin.' |
| 112 | + .' Please create one for the target model: `%s`.', |
| 113 | + $fieldDescription->getName(), |
| 114 | + $fieldDescription->getTargetModel() ?? '' |
| 115 | + )); |
| 116 | + } |
| 117 | + } |
| 118 | + } elseif ($this->isAnyInstanceOf($type, [AdminType::class])) { |
| 119 | + if (!$fieldDescription->hasAssociationAdmin()) { |
| 120 | + throw new \InvalidArgumentException(sprintf( |
| 121 | + 'The current field `%s` is not linked to an admin.' |
| 122 | + .' Please create one for the target model: `%s`.', |
| 123 | + $fieldDescription->getName(), |
| 124 | + $fieldDescription->getTargetModel() ?? '' |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | + if (!$fieldDescription->describesSingleValuedAssociation()) { |
| 129 | + throw new \InvalidArgumentException(sprintf( |
| 130 | + 'You are trying to add `%s` field `%s` which is not a One-To-One or Many-To-One association.' |
| 131 | + .' You SHOULD use `%s` instead.', |
| 132 | + AdminType::class, |
| 133 | + $fieldDescription->getName(), |
| 134 | + CollectionType::class |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + // set sensitive default value to have a component working fine out of the box |
| 139 | + $options['btn_add'] = false; |
| 140 | + $options['delete'] = false; |
| 141 | + |
| 142 | + $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
| 143 | + $options['empty_data'] = static fn (): object => $fieldDescription->getAssociationAdmin()->getNewInstance(); |
| 144 | + $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
| 145 | + } elseif ($this->isAnyInstanceOf($type, [ |
| 146 | + CollectionType::class, |
| 147 | + ])) { |
| 148 | + if (!$fieldDescription->hasAssociationAdmin()) { |
| 149 | + throw new \InvalidArgumentException(sprintf( |
| 150 | + 'The current field `%s` is not linked to an admin.' |
| 151 | + .' Please create one for the target model: `%s`.', |
| 152 | + $fieldDescription->getName(), |
| 153 | + $fieldDescription->getTargetModel() ?? '' |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + $options['type'] = AdminType::class; |
| 158 | + $options['modifiable'] = true; |
| 159 | + $options['type_options'] = $this->getDefaultAdminTypeOptions($fieldDescription, $formOptions); |
| 160 | + } |
| 161 | + |
| 162 | + return $options; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param string|null $type |
| 167 | + * @param array $classes |
| 168 | + * |
| 169 | + * @return bool |
| 170 | + */ |
| 171 | + private function isAnyInstanceOf(?string $type, array $classes): bool |
| 172 | + { |
| 173 | + if (null === $type) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + foreach ($classes as $class) { |
| 178 | + if (is_a($type, $class, true)) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + // handle form type inheritance and check all parent types |
| 184 | + $resolvedType = $this->formRegistry->getType($type); |
| 185 | + $parentType = $resolvedType->getParent(); |
| 186 | + if (null !== $parentType) { |
| 187 | + $parentType = \get_class($parentType->getInnerType()); |
| 188 | + |
| 189 | + // all types have "Symfony\Component\Form\Extension\Core\Type\FormType" as parent |
| 190 | + // so we ignore it here for performance reasons |
| 191 | + if (FormType::class !== $parentType) { |
| 192 | + return $this->isAnyInstanceOf($parentType, $classes); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @param FieldDescriptionInterface $fieldDescription |
| 201 | + * @param array $formOptions |
| 202 | + * |
| 203 | + * @return array |
| 204 | + */ |
| 205 | + private function getDefaultAdminTypeOptions(FieldDescriptionInterface $fieldDescription, array $formOptions): array |
| 206 | + { |
| 207 | + $typeOptions = [ |
| 208 | + 'sonata_field_description' => $fieldDescription, |
| 209 | + 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
| 210 | + 'empty_data' => static fn (): object => $fieldDescription->getAssociationAdmin()->getNewInstance(), |
| 211 | + ]; |
| 212 | + |
| 213 | + if (isset($formOptions['by_reference'])) { |
| 214 | + $typeOptions['collection_by_reference'] = $formOptions['by_reference']; |
| 215 | + } |
| 216 | + |
| 217 | + return $typeOptions; |
| 218 | + } |
| 219 | +} |
0 commit comments