Skip to content

Commit 92ce5c0

Browse files
mikadamczykŁukasz Serwatka
authored and
Łukasz Serwatka
committed
EZP-30297: Error when I try to add a translation for a user object (#282)
1 parent e60e828 commit 92ce5c0

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed

lib/Data/ContentTranslationData.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the eZ RepositoryForms package.
5+
*
6+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
7+
* @license For full copyright and license information view LICENSE file distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace EzSystems\RepositoryForms\Data;
12+
13+
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
14+
15+
/**
16+
* @property \EzSystems\RepositoryForms\Data\Content\FieldData[] $fieldsData
17+
* @property \eZ\Publish\API\Repository\Values\Content\Content $content
18+
*/
19+
class ContentTranslationData extends ContentUpdateStruct implements NewnessCheckable
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function isNew(): bool
25+
{
26+
return false;
27+
}
28+
}

lib/FieldType/Mapper/UserAccountFieldValueFormMapper.php

+48-19
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99
namespace EzSystems\RepositoryForms\FieldType\Mapper;
1010

11+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
1112
use eZ\Publish\Core\FieldType\User\Value as ApiUserValue;
1213
use EzSystems\RepositoryForms\Data\Content\FieldData;
14+
use EzSystems\RepositoryForms\Data\ContentTranslationData;
1315
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
1416
use EzSystems\RepositoryForms\Data\User\UserAccountFieldData;
1517
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
@@ -23,7 +25,6 @@
2325
use Symfony\Component\Form\Exception\UnexpectedTypeException;
2426
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
2527
use Symfony\Component\Form\FormInterface;
26-
use Symfony\Component\OptionsResolver\Exception\AccessException;
2728
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
2829
use Symfony\Component\OptionsResolver\OptionsResolver;
2930
use Symfony\Component\Validator\Constraints\Range;
@@ -50,22 +51,26 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
5051
$formConfig = $fieldForm->getConfig();
5152
$rootForm = $fieldForm->getRoot()->getRoot();
5253
$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);
5372

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());
6974
}
7075

7176
/**
@@ -104,7 +109,9 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
104109
/**
105110
* Fake method to set the translation domain for the extractor.
106111
*
107-
* @throws AccessException
112+
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
113+
*
114+
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
108115
*/
109116
public function configureOptions(OptionsResolver $resolver)
110117
{
@@ -115,9 +122,31 @@ public function configureOptions(OptionsResolver $resolver)
115122
}
116123

117124
/**
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
119148
*/
120-
public function getModelTransformer()
149+
public function getModelTransformer(): CallbackTransformer
121150
{
122151
return new CallbackTransformer(
123152
function (ApiUserValue $data) {

lib/Form/Type/Content/ContentEditType.php

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function configureOptions(OptionsResolver $resolver)
5858
'drafts_enabled' => false,
5959
'data_class' => '\eZ\Publish\API\Repository\Values\Content\ContentStruct',
6060
'translation_domain' => 'ezrepoforms_content',
61+
'intent' => 'update',
6162
]);
6263
}
6364
}

0 commit comments

Comments
 (0)