public function onRenderCustomFieldRule( Field $field, array &$data ) : string

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

$field

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

&$data

(array)  An associative array of display data.

Return Value

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


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   Field   $field  The custom field instance.
 * @param   array   &$data  An array of display data.
 *
 * @return  string  The new layout of the field. Do not return anything
 *                  to keep using the layout defined by the field.
 */
public function onRenderCustomFieldRule($field, &$data)
{
    // make sure we are rendering a field with "country" rule
    if ($field->get('rule') !== 'country') {
        // different rule, do nothing
        return null;
    }

    // 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
    return sprintf(
        '<select name="%s" id="%s" class="%s">%s</select>',
        $data['name'],
        $data['id'],
        $data['class'],
        $options
    );
}

Changelog

Version Description
1.9 Introduced.
Last Update: 2023-12-29 14:15
Helpful?