gppa_template_rows
Description
JavaScript filter to modify the available choice/template templates available in the Form Editor.
Usage
gform.addFilter( 'gppa_input_choice', 'my_custom_function' );
Parameters
templateRows array
The available template rows. Each template should have an “id” string, “label” string, “required” boolean, and “shouldShow” callback function. The “shouldShow” callback function gets two arguments, “field” object and “populate” string.
field object
The current field shown in the Form Editor.
populate string
What’s being populated. Either
choices
orvalues
.
Since
This JavaScript filter is available since Gravity Forms Populate Anything 1.0-beta-4.116.
Examples
Add A Custom Choice Template
Add a choice template named image
. This pairs well with the gppa_input_choice
PHP filter.
<?php
/**
* Gravity Perks // Populate Anything // Add A Custom Choice Template
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Choice templates allow you to map data to different properties of your Gravity Forms choices.
*
* In this example, we demonstrate how to create a new template for a theoretical "image" choice property.
* With this in place, you would be able to select a value in the Populate Anything choices UI that is
* mapped to the "image" property for each choice.
*/
add_action( 'admin_print_footer_scripts', function () {
if ( ! is_callable( 'GFCommon::is_form_editor' ) || ! GFCommon::is_form_editor() ) {
return;
}
?>
<script>
window.gform.addFilter( 'gppa_template_rows', function (templateRows, field, populate) {
if ( populate !== 'choices' ) {
return templateRows;
}
templateRows.push( {
id: 'image',
label: 'Image',
} );
return templateRows;
} );
</script>
<?php
} );
add_filter( 'gppa_input_choice', function( $choice, $field, $object, $objects ) {
$choice['image'] = gp_populate_anything()->process_template( $field, 'image', $object, 'choices', $objects );
return $choice;
}, 10, 4 );
Add A Custom Choice Template That is Conditionally Displayed
Add a choice template named my_fancy_id
that only shows up if the custom field setting my_custom_setting
is truthy.
window.gform.addFilter( 'gppa_template_rows', function ( templateRows, field, populate ) {
if ( populate !== 'choices' ) {
return templateRows;
}
templateRows.push( {
id: 'my_fancy_id',
label: 'My fancy label',
required: false,
shouldShow: function( field, populate ) {
if ( populate !== 'choices' ) {
return false;
}
return ! ! field['my_custom_setting'];
},
} );
return templateRows;
} );