Render Rule
public function onRenderCustomFieldRule(mixed $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
-
(VAPCustomField) The custom field instance.
- &$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 mixed $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;
}
// 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
return sprintf(
'<select name="%s" id="%s" class="%s">%s</select>',
$data['name'],
$data['id'],
$data['class'],
$options
);
}
Example #2
The example below explains how to extend the form validation via javascript for a field assigned to a specific rule.
public function onRenderCustomFieldRule($field, &$data)
{
// make sure we are rendering a field with "ticket" rule
if ($field->get('rule') !== 'ticket')
{
// different rule, do nothing
return;
}
$app = JFactory::getApplication();
if ($app->isClient('site') !== true)
{
// ignore if we are not in the front-end
return;
}
$app->getDocument()->addScriptDeclaration("(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 = $('#{$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);");
}
Changelog
Version | Description |
---|---|
1.7 | Introduced. |