I’m trying to add a field on the General tab for each field.
I followed this tutorial: https://docs.gravityforms.com/gform_field_standard_settings/. It’s partially working.
It adds the field, but when I check the checkbox on a field, all the fields end up with the same setting. So if I have 3 fields and I click on the checkbox in one field, they are all checked. If I uncheck one field, they all get unchecked.
I think the issue is that SetFieldProperty is setting the property for all fields. I see that the form object is available and that I need to set the property there (I can see checking one checkbox sets all fields here), but I don’t know how to get the id of the field being modified so I can set the correct field in the form object.
Here’s my code. I modified it slightly so that all field types have this checkbox.
add_action( 'gform_field_standard_settings', 'gf_award_standard_settings', 10, 2 );
function gf_award_standard_settings( $position, $form_id ) {
//create settings on position 1600 (right before rules)
if ( $position == 1600 ) {
?>
<li class="private_setting field_setting">
<input type="checkbox" id="private" onclick="SetFieldProperty('privateField', this.checked);" />
<label for="private" style="display:inline;">
<?php _e("Keep this field private", "my-plugin"); ?>
<?php gform_tooltip("form_field_private_value") ?>
</label>
</li>
<?php
}
}
//Action to inject supporting script to the form editor page
add_action( 'gform_editor_js', 'gf_editor_script' );
function gf_editor_script(){
?>
<script type='text/javascript'>
//adding setting to all fields
for(const key in fieldSettings) {
fieldSettings[key] = fieldSettings[key] += ', .private_setting';
}
//binding to the load field settings event to initialize the checkbox
jQuery(document).on('gform_load_field_settings', function(event, field, form){
jQuery( '#private' ).prop( 'checked', Boolean( rgar( field, 'privateField' ) ) );
});
</script>
<?php
}
//Filter to add a new tooltip
add_filter( 'gform_tooltips', 'gf_add_private_tooltips' );
function gf_add_private_tooltips( $tooltips ) {
$tooltips['form_field_private_value'] = "<h6>Privacy</h6>Check this box to keep field only available to admins";
return $tooltips;
}