Customer Tracking domain choice required

there is a way to set as "required field" the customer Tracking Domain choice ?
You can do this with hooks, in folder: web/apps/ create file init-custom.php and then here paste bellow code:

PHP:
hooks()->addAction('controller_action_save_data', function(CAttributeCollection $collection) {
    // Make sure you are in the customer area
    if (!apps()->isAppName('customer')) {
        return;
    }

    /** @var ActiveRecord $model */
    $model = $collection->itemAt('campaign');
    $customer = $model->list->customer;
    $canSelectTrackingDomains = $customer->getGroupOption('tracking_domains.can_select_for_campaigns', 'no') == 'yes';

    // Make sure option is enabled
    if (!$canSelectTrackingDomains) {
      return;
    }

    $controller = $collection->itemAt('controller');
    // Continue only for the campaigns and the setup action
    if ($controller->getId() != 'campaigns' || $controller->getAction()->getId() != 'setup') {
        return;
    }

    if (!$collection->itemAt('success')) {
        return;
    }

    // Check if tracking domain is empty
    if (empty($model->option->tracking_domain_id)) {
        // Clear all notifications
        notify()->clearAll();
        // Add notification message to display
        notify()->addError(t('app', 'Your form has a few errors, please fix them and try again!'));
        $collection->success = false;
        $model->option->addError('tracking_domain_id', t('app', 'This field is required'));
    }

});
 
hi @laurentiu ,
after this change, when the user sign in get this error :

Schermata 2022-05-18 alle 20.50.59.png

it seems cause of this raw :
$canSelectTrackingDomains = $customer->getGroupOption('tracking_domains.can_select_for_campaigns', 'no') == 'yes';

have you any idea on how to solve ?
thanks
 
I think this is the correct code:
PHP:
hooks()->addAction('controller_action_save_data', function(CAttributeCollection $collection) {
    // Make sure you are in the customer area and the customer is logged in
    if (!apps()->isAppName('customer') || !customer()->getId()) {
        return;
    }

    $controller = $collection->itemAt('controller');
    
    // Continue only for the campaigns and the setup action
    if (empty($controller) || $controller->getId() != 'campaigns' || $controller->getAction()->getId() != 'setup') {
        return;
    }

    if (!$collection->itemAt('success')) {
        return;
    }

    /** @var ActiveRecord $model */
    $model = $collection->itemAt('campaign');
    $customer = $model->list->customer;
    $canSelectTrackingDomains = $customer->getGroupOption('tracking_domains.can_select_for_campaigns', 'no') == 'yes';

    // Make sure option is enabled
    if (!$canSelectTrackingDomains) {
      return;
    }

    // Check if tracking domain is empty
    if (empty($model->option->tracking_domain_id)) {
        // Clear all notifications
        notify()->clearAll();
        // Add notification message to display
        notify()->addError(t('app', 'Your form has a few errors, please fix them and try again!'));
        $collection->success = false;
        $model->option->addError('tracking_domain_id', t('app', 'This field is required'));
    }

});
 
Back
Top