|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psi\Component\ContentType\Form\Extension\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Psi\Component\ContentType\FieldInterface; |
|
6
|
|
|
use Psi\Component\ContentType\FieldRegistry; |
|
7
|
|
|
use Psi\Component\ContentType\Metadata\ClassMetadata; |
|
8
|
|
|
use Psi\Component\ContentType\Metadata\PropertyMetadata; |
|
9
|
|
|
use Psi\Component\ContentType\OptionsResolver\FieldOptionsResolver; |
|
10
|
|
|
use Symfony\Component\Form\AbstractType; |
|
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
12
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
13
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Surrogate type for virtual user "content" form types. |
|
17
|
|
|
* |
|
18
|
|
|
* For example, if the user manages an "Article" class with the content type |
|
19
|
|
|
* system, this class will act as its form type. |
|
20
|
|
|
*/ |
|
21
|
|
|
class SurrogateType extends AbstractType |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $contentFqn; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ClassMetadata |
|
30
|
|
|
*/ |
|
31
|
|
|
private $classMetadata; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var FieldRegistry |
|
35
|
|
|
*/ |
|
36
|
|
|
private $fieldRegistry; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
$contentFqn, |
|
40
|
|
|
FieldRegistry $fieldRegistry, |
|
41
|
|
|
ClassMetadata $classMetadata |
|
42
|
|
|
) { |
|
43
|
|
|
$this->contentFqn = $contentFqn; |
|
44
|
|
|
$this->fieldRegistry = $fieldRegistry; |
|
45
|
|
|
$this->classMetadata = $classMetadata; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
52
|
|
|
{ |
|
53
|
|
|
$grouped = []; |
|
54
|
|
|
$ungrouped = []; |
|
55
|
|
|
foreach ($this->classMetadata->getPropertyMetadata() as $propertyMetadata) { |
|
56
|
|
|
if (null === $group = $propertyMetadata->getGroup()) { |
|
57
|
|
|
$ungrouped[] = $propertyMetadata; |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!isset($grouped[$group])) { |
|
62
|
|
|
$grouped[$group] = []; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$grouped[$group][] = $propertyMetadata; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->addPropertyMetadatas($builder, $ungrouped); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($grouped as $group => $propertyMetadatas) { |
|
71
|
|
|
if (false === $builder->has($group)) { |
|
72
|
|
|
$builder->add( |
|
73
|
|
|
$builder->create($group, FormType::class, [ |
|
74
|
|
|
'inherit_data' => true, |
|
75
|
|
|
]) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->addPropertyMetadatas($builder->get($group), $propertyMetadatas); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
87
|
|
|
{ |
|
88
|
|
|
$resolver->setDefault('data_class', $this->contentFqn); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function addPropertyMetadatas(FormBuilderInterface $builder, array $propertyMetadatas) |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($propertyMetadatas as $propertyMetadata) { |
|
94
|
|
|
$field = $this->fieldRegistry->get($propertyMetadata->getType()); |
|
95
|
|
|
$builder->add( |
|
96
|
|
|
$propertyMetadata->getName(), |
|
97
|
|
|
$field->getFormType(), |
|
98
|
|
|
$this->getFormOptions($field, $propertyMetadata) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function getFormOptions(FieldInterface $field, PropertyMetadata $propertyMetadata) |
|
104
|
|
|
{ |
|
105
|
|
|
$formOptions = $propertyMetadata->getOptions(); |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
$resolver = new FieldOptionsResolver(); |
|
109
|
|
|
$field->configureOptions($resolver); |
|
110
|
|
|
|
|
111
|
|
|
return $resolver->resolveFormOptions($formOptions); |
|
112
|
|
|
} catch (\Exception $e) { |
|
113
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
114
|
|
|
'Could not resolve options for property "%s#%s" (type "%s")', |
|
115
|
|
|
$propertyMetadata->getClass(), $propertyMetadata->getName(), $propertyMetadata->getType() |
|
116
|
|
|
), null, $e); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|