Important
You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.
See our Release Process documentation for more information on the currently supported and upcoming releases.
Add the Customer Consents Field to a Form (Example)¶
Mapped Field¶
If an entity contains the customerUser property, add the customerConsents field to the form using property_path in the form_options.
1$form->add(
2 ConsentAcceptanceType::TARGET_FIELDNAME,
3 ConsentAcceptanceType::class,
4 [
5 'property_path' => 'customerUser.acceptedConsents',
6 'constraints' => [
7 new RemovedLandingPages(),
8 new RemovedConsents(),
9 new RequiredConsents()
10 ]
11 ]
12);
Non-Mapped Field¶
If an entity does not contain the customerUser property:
Add the customerConsents field to the form and set mapped = false in the form_options.
1$form->add( 2 ConsentAcceptanceType::TARGET_FIELDNAME, 3 ConsentAcceptanceType::class, 4 [ 5 'mapped' => false, 6 'constraints' => [ 7 new RemovedLandingPages(), 8 new RemovedConsents(), 9 new RequiredConsents() 10 ] 11 ] 12);
Create a form listener service.
1acme_demo.event_listener.form_listener: 2 class: Acme\Bundle\DemoBundle\EventListener\BeforeFlushFormListener 3 lazy: true 4 tags: 5 - { name: kernel.event_listener, event: oro.form.update_handler.before_entity_flush.__FORM_NAME__, method: beforeFlush } 6 - { name: oro_featuretogle.feature, feature: consents }
Implement the logic before the flush event.
1namespace Acme\Bundle\DemoBundle\EventListener; 2 3use Oro\Bundle\ConsentBundle\Form\Type\ConsentAcceptanceType; 4use Oro\Bundle\FeatureToggleBundle\Checker\FeatureCheckerHolderTrait; 5use Oro\Bundle\FormBundle\Event\FormHandler\AfterFormProcessEvent; 6 7class BeforeFlushFormListener 8{ 9 use FeatureCheckerHolderTrait; 10 11 /** 12 * @param AfterFormProcessEvent $event 13 */ 14 public function beforeFlush(AfterFormProcessEvent $event) 15 { 16 // No actions if consents feature disabled 17 if (!$this->isFeaturesEnabled()) { 18 return; 19 } 20 21 $formData = $event->getData(); 22 23 if ($formData instanceof Request) { 24 $customerUser = $formData->getCustomerUser(); 25 if ($customerUser && $customerUser->isGuest()) { 26 $form = $event->getForm(); 27 $acceptedConsents = $form->get(ConsentAcceptanceType::TARGET_FIELDNAME)->getData(); 28 29 $customerUser->setAcceptedConsents($acceptedConsents); 30 } 31 } 32 } 33}
Render Form Field in the Storefront¶
First, check that customerConsents is rendered in the form template, the input with type hidden should be rendered on the page.
1 {% if form.customerConsents is defined %}
2 {{ form_widget(form.customerConsents) }}
3 {% endif %}
To show a block with consent items, import the layout with consent items and configure it.
1 layout:
2 imports:
3 -
4 id: oro_consent_items
5 root: consent_container
6
7 actions:
8 - '@setBlockTheme':
9 themes: 'consents.html.twig'
10
11 - '@add':
12 id: consent_container
13 blockType: container
14 parentId: __PARENT_BLOCK_ID__
15
16 - '@add':
17 id: consent_message
18 blockType: consent_acceptance_choice
19 parentId: consent_container
If all consents are accepted, add a template with a success message.
1 {% block _checkout_consent_message_widget %}
2 {% set attr = layout_attr_defaults(attr, {
3 'class': 'notification notification--success'
4 }) %}
5
6 {% if consents is empty %}
7 <div {{ block('block_attributes') }}>
8 <span class="notification__item"><i class="fa-check"></i> {{ 'All mandatory consents were accepted.' }}</span>
9 </div>
10 {% endif %}
11 {% endblock %}