Skip to content

Commit b574aef

Browse files
committed
Extending wizard
1 parent 8b2e417 commit b574aef

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

docs/discounts/extend_discounts_wizard.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,51 @@ The custom condition is now integrated with the discounts wizard and can be used
107107

108108
This example continues the [purchasing power parity rule example](extend_discounts.md#implement-custom-rules), integrating the rule with the wizard.
109109

110+
First, we need to create a new service implementing the `DiscountValueMapperInterface` interface, responsible for handling the new rule type:
110111

112+
``` php hl_lines="59-60"
113+
[[= include_file('code_samples/discounts/src/Form/FormMapper/PurchasingPowerParityValueMapper.php') =]]
114+
```
115+
116+
It uses an `PurchasingPowerParityValue` object to store the form data.
117+
In the example, the data about discount value is stored directly in rule's code and the object does not require any additional properties.
118+
119+
120+
``` php
121+
[[= include_file('code_samples/discounts/src/Form/Data/PurchasingPowerParityValue.php', 0, 9) =]]
122+
```
123+
124+
This value mapper is used by a new form mapper, dedicated to the new rule type:
125+
126+
``` php
127+
[[= include_file('code_samples/discounts/src/Form/FormMapper/PurchasingPowerParityFormMapper.php') =]]
128+
```
129+
130+
Link them together when defining the services:
131+
132+
``` yaml
133+
App\Form\FormMapper\PurchasingPowerParityValueMapper: ~
134+
135+
App\Form\FormMapper\PurchasingPowerParityFormMapper:
136+
arguments:
137+
$discountValueMapper: '@App\Form\FormMapper\PurchasingPowerParityValueMapper'
138+
```
139+
140+
The [`DiscountFormMapperInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-DiscountFormMapperInterface.html) acts as a registry, finding a form mapper dedicated for given rule type and delegating to the the responsibility of building the form.
141+
142+
As each rule type might have a different rule calculation logic, each rule must have a different "Discount value" step in the form.
143+
144+
To create it, create a dedicated class implementing the [`DiscountValueFormTypeMapperInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Discounts-Admin-Form-DiscountValueFormTypeMapperInterface.html)
145+
146+
``` php
147+
[[= include_file('code_samples/discounts/src/Form/FormMapper/PurchasingPowerParityDiscountValueFormTypeMapper.php') =]]
148+
```
149+
150+
and add a dedicated value type class:
151+
152+
``` php hl_lines="26-40 47-61 72"
153+
[[= include_file('code_samples/discounts/src/Form/Type/DiscountValue/PurchasingPowerParityValueType.php') =]]
154+
```
155+
156+
In the example above, the discount value step is used to to display a read-only field with regions the discount is limited to.
157+
The `$availableRegionHandler` callback function extracts the selected regions and modifies the form as needed, using the `FormEvents::PRE_SET_DATA` and `FormEvents::POST_SUBMIT` events.

0 commit comments

Comments
 (0)