Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Campi  >  Render Rule

apply_filters_ref_array( 'vikrestaurants_render_custom_field_rule', string $html, Field $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

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

&$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  Field   $field  The custom field instance.
 * @param  array   &$data  An array of display data.
 */
add_filter('vikrestaurants_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::_('vrehtml.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);

Changelog

Version Description
1.3 Introduced.
Ultimo aggiornamento: 2023-12-28
Utile?
Potrebbe interessarti: