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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$form->add(
    ConsentAcceptanceType::TARGET_FIELDNAME,
    ConsentAcceptanceType::class,
    [
        'property_path' => 'customerUser.acceptedConsents',
        'constraints' => [
            new RemovedLandingPages(),
            new RemovedConsents(),
            new RequiredConsents()
        ]
    ]
);

Non-Mapped Field

If an entity does not contain the customerUser property:

  1. Add the customerConsents field to the form and set mapped = false in the form_options.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    $form->add(
        ConsentAcceptanceType::TARGET_FIELDNAME,
        ConsentAcceptanceType::class,
        [
            'mapped' => false,
            'constraints' => [
                new RemovedLandingPages(),
                new RemovedConsents(),
                new RequiredConsents()
            ]
        ]
    );
    
  2. Create a form listener service.

    1
    2
    3
    4
    5
    6
    acme_demo.event_listener.form_listener:
        class: Acme\Bundle\DemoBundle\EventListener\BeforeFlushFormListener
        lazy: true
        tags:
            - { name: kernel.event_listener, event: oro.form.update_handler.before_entity_flush.__FORM_NAME__, method: beforeFlush }
            - { name: oro_featuretogle.feature, feature: consents }
    
  3. Implement the logic before the flush event.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    namespace Acme\Bundle\DemoBundle\EventListener;
    
    use Oro\Bundle\ConsentBundle\Form\Type\ConsentAcceptanceType;
    use Oro\Bundle\FeatureToggleBundle\Checker\FeatureCheckerHolderTrait;
    use Oro\Bundle\FormBundle\Event\FormHandler\AfterFormProcessEvent;
    
    class BeforeFlushFormListener
    {
        use FeatureCheckerHolderTrait;
    
        /**
         * @param AfterFormProcessEvent $event
         */
        public function beforeFlush(AfterFormProcessEvent $event)
        {
            // No actions if consents feature disabled
            if (!$this->isFeaturesEnabled()) {
                return;
            }
    
            $formData = $event->getData();
    
            if ($formData instanceof Request) {
                $customerUser = $formData->getCustomerUser();
                if ($customerUser && $customerUser->isGuest()) {
                    $form = $event->getForm();
                    $acceptedConsents = $form->get(ConsentAcceptanceType::TARGET_FIELDNAME)->getData();
    
                    $customerUser->setAcceptedConsents($acceptedConsents);
                }
            }
        }
    }
    

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
2
3
 {% if form.customerConsents is defined %}
     {{ form_widget(form.customerConsents) }}
 {% endif %}

To show a block with consent items, import the layout with consent items and configure it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 layout:
     imports:
         -
             id: oro_consent_items
             root: consent_container

     actions:
         - '@setBlockTheme':
             themes: 'consents.html.twig'

         - '@add':
             id: consent_container
             blockType: container
             parentId: __PARENT_BLOCK_ID__

         - '@add':
             id: consent_message
             blockType: consent_acceptance_choice
             parentId: consent_container

If all consents are accepted, add a template with a success message.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 {% block _checkout_consent_message_widget %}
     {% set attr = layout_attr_defaults(attr, {
         'class': 'notification notification--success'
     }) %}

     {% if consents is empty %}
         <div {{ block('block_attributes') }}>
             <span class="notification__item"><i class="fa-check"></i> {{ 'All mandatory consents were accepted.' }}</span>
         </div>
     {% endif %}
 {% endblock %}