1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Form\Factory; |
6
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
8
|
|
|
use LAG\AdminBundle\Factory\FieldFactoryInterface; |
9
|
|
|
use LAG\AdminBundle\Utils\FormUtils; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
11
|
|
|
use Symfony\Component\Form\FormFactoryInterface as SymfonyFormFactoryInterface; |
12
|
|
|
use Symfony\Component\Form\FormInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class FormFactory implements FormFactoryInterface |
16
|
|
|
{ |
17
|
|
|
public function __construct( |
18
|
|
|
private SymfonyFormFactoryInterface $formFactory, |
19
|
|
|
private FieldFactoryInterface $fieldFactory |
20
|
|
|
) { |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function createEntityForm(AdminInterface $admin, Request $request, object $data): FormInterface |
24
|
|
|
{ |
25
|
|
|
$action = $admin->getAction(); |
26
|
|
|
$formType = $action->getConfiguration()->getForm(); |
27
|
|
|
|
28
|
|
|
if ($formType !== null) { |
29
|
|
|
return $this->formFactory->create($formType, $data, $action->getConfiguration()->getFormOptions()); |
30
|
|
|
} |
31
|
|
|
$formBuilder = $this->formFactory->createBuilder(FormType::class, $data, [ |
32
|
|
|
'label' => false, |
33
|
|
|
]); |
34
|
|
|
$fieldDefinitions = $this->fieldFactory->createDefinitions(\get_class($data)); |
35
|
|
|
|
36
|
|
|
foreach ($fieldDefinitions as $name => $definition) { |
37
|
|
|
// We do not want to edit those values in a Form |
38
|
|
|
if (\in_array($name, ['createdAt', 'updatedAt']) && 'datetime' === $definition->getType()) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (in_array($name, ['id', 'identifier', 'uuid'])) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
$formType = FormUtils::convertShortFormType($definition->getType()); |
46
|
|
|
$formOptions = $definition->getFormOptions(); |
47
|
|
|
$formBuilder->add($name, $formType, $formOptions); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $formBuilder->getForm(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function createDeleteForm(AdminInterface $admin, Request $request, $data): FormInterface |
54
|
|
|
{ |
55
|
|
|
$action = $admin->getAction(); |
56
|
|
|
$actionConfiguration = $action->getConfiguration(); |
57
|
|
|
|
58
|
|
|
return $this |
59
|
|
|
->formFactory |
60
|
|
|
->create($actionConfiguration->getForm(), $data, $actionConfiguration->getFormOptions()) |
|
|
|
|
61
|
|
|
; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|