8
8
*/
9
9
namespace EzSystems \RepositoryForms \FieldType \Mapper ;
10
10
11
+ use eZ \Publish \API \Repository \Values \ContentType \FieldDefinition ;
11
12
use eZ \Publish \Core \FieldType \User \Value as ApiUserValue ;
12
13
use EzSystems \RepositoryForms \Data \Content \FieldData ;
14
+ use EzSystems \RepositoryForms \Data \ContentTranslationData ;
13
15
use EzSystems \RepositoryForms \Data \FieldDefinitionData ;
14
16
use EzSystems \RepositoryForms \Data \User \UserAccountFieldData ;
15
17
use EzSystems \RepositoryForms \FieldType \FieldDefinitionFormMapperInterface ;
23
25
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
24
26
use Symfony \Component \Form \Extension \Core \Type \IntegerType ;
25
27
use Symfony \Component \Form \FormInterface ;
26
- use Symfony \Component \OptionsResolver \Exception \AccessException ;
27
28
use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
28
29
use Symfony \Component \OptionsResolver \OptionsResolver ;
29
30
use Symfony \Component \Validator \Constraints \Range ;
@@ -50,22 +51,26 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
50
51
$ formConfig = $ fieldForm ->getConfig ();
51
52
$ rootForm = $ fieldForm ->getRoot ()->getRoot ();
52
53
$ formIntent = $ rootForm ->getConfig ()->getOption ('intent ' );
54
+ $ isTranslation = $ rootForm ->getData () instanceof ContentTranslationData;
55
+ $ formBuilder = $ formConfig ->getFormFactory ()->createBuilder ()
56
+ ->create ('value ' , UserAccountFieldType::class, [
57
+ 'required ' => true ,
58
+ 'label ' => $ fieldDefinition ->getName (),
59
+ 'intent ' => $ formIntent ,
60
+ 'constraints ' => [
61
+ new UserAccountPassword (['contentType ' => $ rootForm ->getData ()->contentType ]),
62
+ ],
63
+ ]);
64
+
65
+ if ($ isTranslation ) {
66
+ $ formBuilder ->addModelTransformer ($ this ->getModelTransformerForTranslation ($ fieldDefinition ));
67
+ } else {
68
+ $ formBuilder ->addModelTransformer ($ this ->getModelTransformer ());
69
+ }
70
+
71
+ $ formBuilder ->setAutoInitialize (false );
53
72
54
- $ fieldForm
55
- ->add (
56
- $ formConfig ->getFormFactory ()->createBuilder ()
57
- ->create ('value ' , UserAccountFieldType::class, [
58
- 'required ' => true ,
59
- 'label ' => $ fieldDefinition ->getName (),
60
- 'intent ' => $ formIntent ,
61
- 'constraints ' => [
62
- new UserAccountPassword (['contentType ' => $ rootForm ->getData ()->contentType ]),
63
- ],
64
- ])
65
- ->addModelTransformer ($ this ->getModelTransformer ())
66
- ->setAutoInitialize (false )
67
- ->getForm ()
68
- );
73
+ $ fieldForm ->add ($ formBuilder ->getForm ());
69
74
}
70
75
71
76
/**
@@ -104,7 +109,9 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
104
109
/**
105
110
* Fake method to set the translation domain for the extractor.
106
111
*
107
- * @throws AccessException
112
+ * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
113
+ *
114
+ * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
108
115
*/
109
116
public function configureOptions (OptionsResolver $ resolver )
110
117
{
@@ -115,9 +122,31 @@ public function configureOptions(OptionsResolver $resolver)
115
122
}
116
123
117
124
/**
118
- * @return CallbackTransformer
125
+ * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
126
+ *
127
+ * @return \Symfony\Component\Form\CallbackTransformer
128
+ */
129
+ public function getModelTransformerForTranslation (FieldDefinition $ fieldDefinition ): CallbackTransformer
130
+ {
131
+ return new CallbackTransformer (
132
+ function (ApiUserValue $ data ) {
133
+ return new UserAccountFieldData ($ data ->login , null , $ data ->email , $ data ->enabled );
134
+ },
135
+ function (UserAccountFieldData $ submittedData ) use ($ fieldDefinition ) {
136
+ $ userValue = clone $ fieldDefinition ->defaultValue ;
137
+ $ userValue ->login = $ submittedData ->username ;
138
+ $ userValue ->email = $ submittedData ->email ;
139
+ $ userValue ->enabled = $ submittedData ->enabled ;
140
+
141
+ return $ userValue ;
142
+ }
143
+ );
144
+ }
145
+
146
+ /**
147
+ * @return \Symfony\Component\Form\CallbackTransformer
119
148
*/
120
- public function getModelTransformer ()
149
+ public function getModelTransformer (): CallbackTransformer
121
150
{
122
151
return new CallbackTransformer (
123
152
function (ApiUserValue $ data ) {
0 commit comments