English (United Kingdom)
Knowledge Base  >  Vik Appointments  >  Hooks  >  Fields  >  Render Rule

apply_filters_ref_array( 'vikappointments_render_custom_field_rule', string $html, mixed $field, array &$data )

Fires before rendering a custom field.


Description

Trigger hook to allow external plugins to manipulate the data to display or the type of layout to render.

In case one of the attached plugins returned a non-empty string, then the field will use it as HTML in place of the default layout.

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


Parameters

$html

(string)  The new layout of the field. Do not return anything to keep using the layout defined by the field.

$field

(VAPCustomField)  The custom field instance.

&$data

(array)  An associative array of display data.


Example

The example below displays a dropdown of countries for those fields that picked our new "Country" rule.

/**
 * Trigger hook to allow external plugins to manipulate the data to
 * display or the type of layout to render. In case one of the attached
 * plugins returned a string, then the field will use it as HTML in
 * place of the default layout.
 *
 * @param  string  $html   The new layout of the field. Do not return anything
 *                         to keep using the layout defined by the field.
 * @param  mixed   $field  The custom field instance.
 * @param  array   &$data  An array of display data.
 */
add_filter('vikappointments_render_custom_field_rule', function($html, $field, &$data)
{
    // make sure we are rendering a field with "country" rule
    if ($field->get('rule') !== 'country')
    {
        // different rule, do nothing
        return $html;
    }

    // get list of supported countries
    $countries = JHtml::_('vaphtml.countries.getlist', $order = 'country_name');

    // build options
    $options = JHtml::_('select.options', $countries, 'code2', 'name', $data['value']);

    // add a placeholder at the beginning
    $options = "<option></option>\n" . $options;

    // build dropdown HTML
    $html .= sprintf(
        '<select name="%s" id="%s" class="%s">%s</select>',
        $data['name'],
        $data['id'],
        $data['class'],
        $options
    );

    return $html;
}, 10, 3);

Example #2

The example below explains how to extend the form validation via javascript for a field assigned to a specific rule.

add_filter('vikappointments_render_custom_field_rule', function($html, $field, &$data)
{
    // make sure we are rendering a field with "ticket" rule
    if ($field->get('rule') !== 'ticket')
    {
        // different rule, do nothing
        return $html;
    }

    // register validation script for the front-end only
    add_action('wp_print_footer_scripts', function() use ($data)
    {
        ?>
<script>
(function($) {
    'use strict';

    onInstanceReady(() => {
        // wait until the custom fields validator is ready
        if (typeof vapCustomFieldsValidator === 'undefined') {
            return false;
        }

        return vapCustomFieldsValidator;
    }).then((form) => {
        // register a new validation callback at runtime
        form.addCallback(() => {
            // take our ticket field by ID
            const field = $('#<?php echo $data['id']; ?>');

            // make sure the specified ticket is well-formed (to be adjusted according to your needs)
            if (field.val().match(/regex_goes_here/)) {
                // field valid, unset the invalid status previously set (if any)
                form.unsetInvalid(field);
                return true;
            }

            // field invalid
            form.setInvalid(field);
            // display further information to the user
            alert('The specified ticket does not seem to be valid.');
            return false;
        });
    });
})(jQuery);
</script>
        <?php
    });

    return $html;
}, 10, 3);

Changelog

Version Description
1.2 Introduced.
Last Update: 2023-12-15
Helpful?
100% of people found this helpful.

This website uses cookies

This website uses cookies and similar technologies. Some of these technologies are necessary for the proper functioning of the website (Essential). Other technologies are used to evaluate user behavior (Analysis), to integrate external media or for advertising purposes. If you agree, these technologies are activated. For details, please refer to the privacy policy. From this same page you can also update/revoke your consent. If you do not give your consent, only cookies with essential functions will be activated.

We use technologies from the USA on our website. If you give your consent, you therefore consent at the same time to your data being transferred to the USA. We would like to point out that the USA does not have a level of data protection comparable to that of the EU and that any US company can be obliged by state authorities to hand over your data without you having any effective legal protection against this.