English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Fields  >  Dispatch Rule

apply_filters_ref_array( 'vikrestaurants_dispatch_custom_field_rule', bool $did, mixed $field, mixed $value, array &$data )

Fires before dispatching a custom field rule.


Description

Trigger hook to allow external plugins to dispatch a custom rule during the saving process.

This process is mainly used to collect the details specified by the customers into different database tables.

In case the rule needs to validate the entered data, it is possible to throw an exception to abort the saving process.

It is possible to access the rule of the field with $field->get('rule').


Parameters

$did

(bool)  True to avoid dispatching the default system rules.

$field

(Field)  The custom field instance. This class is part of the E4J\VikRestaurants\CustomFields namespace.

$value

(mixed)  The value of the field set in request.

&$data

(array)  The array data to fill-in in case of specific rules (name, e-mail, etc...).


Example

The example registers the specified country code into the customer database table.

/**
 * Trigger hook to allow external plugins to dispatch a custom rule.
 *
 * @param  bool   $did    True to avoid dispatching the default system rules.
 * @param  Field  $field  The custom field instance.
 * @param  mixed  $value  The value of the field set in request.
 * @param  array  &$data  The array data to fill-in in case of
 *                          specific rules (name, e-mail, etc...).
 */
add_filter('vikrestaurants_dispatch_custom_field_rule', function($did, $field, $value, &$data) {
    // make sure we are dispatching a field with "country" rule
    if ($field->get('rule') !== 'country' || $did) {
        // different rule or already completed
        return (bool) $did;
    }

    // register country code within the array data to be saved into the customer record
    $data['country_code'] = $value;

    return true;
}, 10, 4);

Changelog

Version Description
1.3 Introduced.
Last Update: 2023-12-28
Helpful?