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