Skip to content

Commit 080a6e4

Browse files
authored
implement input transformer (#369)
* implement input transformer * translate funnel action label in backend view
1 parent 293613f commit 080a6e4

27 files changed

+343
-37
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade Notes
22

3+
## Version 4.2.1
4+
- **[ENHANCEMENT]**: Input Transformer [#7bde83b](https://github.com/dachcom-digital/pimcore-formbuilder/pull/369/commits/7bde83b015d242f2ce2f93fcd0b4feb78431587c)
5+
- **[BUGFIX]**: Translate Funnel Action Label in Backend [#b4966a2](https://github.com/dachcom-digital/pimcore-formbuilder/pull/369/commits/b4966a2f23c0aa55e1df214632984ff0164aa945)
6+
37
## Version 4.2.0
48
- **[NEW FEATURE]**: Funnel Feature [#362](https://github.com/dachcom-digital/pimcore-formbuilder/issues/362)
59
- **[FEATURE]**: PIMCORE 10.5 Support only!

docs/40_CustomFormType.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ form_builder:
1414

1515
# define the class
1616
class: Symfony\Component\Form\Extension\Core\Type\ChoiceType
17+
18+
# If you need some custom transformers, uncomment these lines:
19+
#
20+
#output_transformer: my_custom_output_transformer
21+
#input_transformer: my_custom_input_transformer
22+
1723
backend:
1824

1925
# you either use one of those fields

docs/OutputWorkflow/09_ApiChannel.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ it's quite easy to integrate your own api provider, read more about it [here](./
1515

1616
## Available Options
1717

18-
| Name | Type | Description |
19-
|------|-------------|-------------|
20-
| Api Provider| `ApiProviderInterface` | Select your API Provider. |
21-
| Options | `mixed` | If available, various provider configuration fields |
18+
| Name | Type | Description |
19+
|--------------|------------------------|-----------------------------------------------------|
20+
| Api Provider | `ApiProviderInterface` | Select your API Provider. |
21+
| Options | `mixed` | If available, various provider configuration fields |
2222

2323
## Mapping
2424
![image](https://user-images.githubusercontent.com/700119/145618709-686d5022-1ed9-4722-9600-0d41eccf55a3.png)

docs/OutputWorkflow/0_Usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Want to learn more? Let's start with the [Email Channel](./10_EmailChannel.md).
3838
- [Email Channel](./10_EmailChannel.md)
3939
- [Object Channel](./11_ObjectChannel.md)
4040
- [Custom Channel](./12_CustomChannel.md)
41+
- [Input Transformer](./14_InputTransformer.md)
4142
- [Output Transformer](./15_OutputTransformer.md)
4243
- [Success Management](./20_SuccessManagement.md)
4344
- [Events](./30_Events.md)

docs/OutputWorkflow/12_CustomChannel.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ To create a custom output workflow channel, you need to create some configuratio
44
## Service Definition
55

66
```yaml
7-
87
services:
98
App\FormBuilder\MyChannel:
109
tags:
1110
- { name: form_builder.output_workflow.channel, type: myChannel }
12-
1311
```
12+
1413
## Output Transformer
1514
Read [here](./15_OutputTransformer.md#custom-output-transformer) how to add a single output transformer to your new custom channel.
1615
1716
## PHP Configuration Form Type Class
18-
1917
```php
2018
<?php
2119

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Input Transformer
2+
3+
Input Transformers will be applied, before a form gets pre-populated with given data.
4+
5+
Mostly this happens, if you're using a funnel output workflow.
6+
There, the form data gets normalized and needs to be transformed back, before we can populate a form with data.
7+
8+
## Available Core Input Transformer
9+
10+
| Available Transformer | Field | Service |
11+
|-----------------------|----------------------------------------|-------------------------------------------------------------------|
12+
| `date_transformer` | `date`, `datetime`, `time`, `birthday` | `@FormBuilderBundle\Transformer\Output\DateDataObjectTransformer` |
13+
14+
> **Don't get confused!**
15+
> Some core output transformers also supports input transformation.
16+
> Since we won't the namespace that because of BC reasons, we'll keep it that way (for now).
17+
18+
## Override default Input Transformer
19+
20+
If you want to transform the text field value for example, you need to add your own transformer.
21+
First, let FormBuilder know about your transformer.
22+
23+
> You're also able to set up your custom input transform for your dynamic fields via the `input_transformer` configuration node.
24+
25+
```yaml
26+
form_builder:
27+
types:
28+
text:
29+
input_transformer: text_input_transformer
30+
```
31+
32+
Now you need to register your new transformer:
33+
34+
```yaml
35+
App\OutputTransformer\TextInputTransformer:
36+
autowire: true
37+
tags:
38+
- { name: form_builder.transformer.input, type: text_input_transformer }
39+
```
40+
41+
***
42+
43+
After that, you have to set up a PHP-Service:
44+
45+
```php
46+
<?php
47+
48+
namespace App\InputTransformer;
49+
50+
use FormBuilderBundle\Model\FieldDefinitionInterface;
51+
use FormBuilderBundle\Transformer\Input\InputTransformerInterface;
52+
53+
class TextInputTransformer implements InputTransformerInterface
54+
{
55+
public function getValueReverse(FieldDefinitionInterface $fieldDefinition, mixed $formValue): string
56+
{
57+
// manipulate or change the value
58+
return $rawValue;
59+
}
60+
}
61+
```
62+
63+
***
64+
65+
## Custom input transformer
66+
67+
After you've created a [custom form type](./../40_CustomFormType.md) (and/or you need it in a funnel output chanel),
68+
you may want to control the input values too. Configure your PHP Service same as explained above.
69+
70+
```yaml
71+
form_builder:
72+
types:
73+
your_custom_type:
74+
input_transformer: custom_input_transformer
75+
76+
services:
77+
App\OutputTransformer\MyCustomInputTransformer:
78+
autowire: true
79+
tags:
80+
- { name: form_builder.transformer.input, type: custom_input_transformer }
81+
```

docs/OutputWorkflow/15_OutputTransformer.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ If no transformer has been found, the `fallback_transformer` will be used.
1616
If you want to transform the text field value for example, you need to add your own transformer.
1717
First, let FormBuilder know about your transformer.
1818

19-
> You're also able to set up your custom output transform for your dynamic fields via `output_transformer` in the optional option configuration node.
19+
> You're also able to set up your custom input transform for your dynamic fields via the `input_transformer` configuration node.
2020
2121
```yaml
2222
form_builder:
@@ -60,31 +60,19 @@ use FormBuilderBundle\Transformer\Output\OutputTransformerInterface;
6060

6161
class TextInputTransformer implements OutputTransformerInterface
6262
{
63-
/**
64-
* @var Translator
65-
*/
66-
protected $translator;
63+
protected Translator $translator;
6764

68-
/**
69-
* @param Translator $translator
70-
*/
7165
public function __construct(Translator $translator)
7266
{
7367
$this->translator = $translator;
7468
}
7569

76-
/**
77-
* {@inheritDoc}
78-
*/
7970
public function getValue(FieldDefinitionInterface $field, FormInterface $formField, $rawValue, $locale)
8071
{
8172
// manipulate or change the value
8273
return $rawValue;
8374
}
8475

85-
/**
86-
* {@inheritDoc}
87-
*/
8876
public function getLabel(FieldDefinitionInterface $field, FormInterface $formField, $rawValue, $locale)
8977
{
9078
// manipulate or change the label
@@ -99,10 +87,10 @@ class TextInputTransformer implements OutputTransformerInterface
9987

10088
After you've created a [custom output channel](./12_CustomChannel.md), you may want to control the output values too.
10189
It is possible to use one transformer for all available fields, like we'll show you below.
102-
If you want to add a fallback to all the other fields in your channel, you need to add an additional tag with type `fallback_transformer`.
90+
If you want to add a fallback to all the other fields in your channel, you need to add a tag with type `fallback_transformer`.
91+
92+
> **Note:** If you don't add any transformer, the `fallback_transformer` will be used again.
10393
104-
> **Note:** If you don't add any transformer, the `fallback_transformer` will be used again.
105-
>
10694
```yaml
10795
App\OutputTransformer\MyChannelOutputTransformer:
10896
autowire: true

docs/OutputWorkflow/40_Funnels.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Below you'll find a brief walk through to get the idea behind this powerful feat
3232
- Every funnel layer receives the `SubmissionEvent` object, so you're able to process users data which has been collected via form builders root form
3333
- Every funnel layer will be submitted as a form. If a funnel layer provides some additional form data, it will be stored within the storage provider via `FunnelRuntimeData`
3434
- After the last channel has been called OR an exception raised, a `funnel_finished` flag will be added to the url. This indicates, that the workflow is done and the current storage will be flushed
35+
- If you're using some custom form fields, make sure you've also defined an [Input Transformer](./14_InputTransformer.md)!
3536

3637
***
3738

src/FormBuilderBundle/Assembler/FormAssembler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public function assembleViewVars(FormOptionsResolver $optionsResolver): array
4141
$exceptionMessage = null;
4242
$formDefinition = null;
4343

44-
$formAssembleEvent = new FormAssembleEvent($optionsResolver);
45-
$this->eventDispatcher->dispatch($formAssembleEvent, FormBuilderEvents::FORM_ASSEMBLE_PRE);
46-
4744
$formId = $optionsResolver->getFormId();
4845

4946
if ($formId !== null) {
@@ -73,6 +70,9 @@ public function assembleViewVars(FormOptionsResolver $optionsResolver): array
7370
return $viewVars;
7471
}
7572

73+
$formAssembleEvent = new FormAssembleEvent($optionsResolver, $formDefinition);
74+
$this->eventDispatcher->dispatch($formAssembleEvent, FormBuilderEvents::FORM_ASSEMBLE_PRE);
75+
7676
$systemRuntimeData = [
7777
'form_preset' => $optionsResolver->getFormPreset(),
7878
'form_output_workflow' => $optionsResolver->getOutputWorkflow(),
@@ -93,7 +93,7 @@ public function assembleViewVars(FormOptionsResolver $optionsResolver): array
9393

9494
$form = $this->frontendFormBuilder->buildForm($formDefinition, $formRuntimeData, $formAssembleEvent->getFormData());
9595

96-
$formAssembleEvent = new FormAssembleEvent($optionsResolver, $form);
96+
$formAssembleEvent = new FormAssembleEvent($optionsResolver, $formDefinition, $form);
9797
$this->eventDispatcher->dispatch($formAssembleEvent, FormBuilderEvents::FORM_ASSEMBLE_POST);
9898

9999
$viewVars['form'] = $form->createView();

src/FormBuilderBundle/Controller/Admin/OutputWorkflowFunnelController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Pimcore\Bundle\AdminBundle\Controller\AdminController;
1010
use Symfony\Component\HttpFoundation\JsonResponse;
1111
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1213
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1314
use Symfony\Component\Serializer\SerializerInterface;
1415

@@ -51,7 +52,18 @@ public function getFunnelLayersAction(Request $request): JsonResponse
5152
'configuration' => [
5253
'dynamicFunnelActionAware' => $service->dynamicFunnelActionAware(),
5354
'funnelActionDefinitions' => $this->serializer instanceof NormalizerInterface
54-
? $this->serializer->normalize($funnelActionDefinitions, 'array', ['groups' => ['ExtJs']])
55+
? $this->serializer->normalize(
56+
$funnelActionDefinitions,
57+
'array',
58+
[
59+
'groups' => ['ExtJs'],
60+
AbstractNormalizer::CALLBACKS => [
61+
'label' => function ($data) {
62+
return $this->trans($data, [], 'messages');
63+
}
64+
]
65+
]
66+
)
5567
: []
5668
]
5769
];

0 commit comments

Comments
 (0)