Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spivurno/c9dc0b98356608828d15bc258dcbcef3 to your computer and use it in GitHub Desktop.
Save spivurno/c9dc0b98356608828d15bc258dcbcef3 to your computer and use it in GitHub Desktop.
Gravity Perks // GP Read Only + GP Copy Cat // Only Disable Checkboxes Checked by GP Copy Cat
<?php
/**
* Gravity Perks // GP Read Only + GP Copy Cat // Only Disable Checkboxes Checked by GP Copy Cat
* http://gravitywiz.com/documentation/gp-read-only/
* http://gravitywiz.com/documentation/gp-copy-cat/
*
* Instructions:
*
* 1 - Copy and paste the snippet into your theme's functions.php file.
* 2 - Add the 'gw-set-checked-as-read-only' in the CSS Class Name setting for the desired field.
* 3 - Enable the Read Only setting on the Perks tab for the same field.
*
* This pairs well with GP Copy Cat where you're copying one checkbox field to another. The target field can then
* have additional checkboxes checked but the copied checkboxes are "locked". Also pairs well with GP Limit Checkboxes
* where you want to limit the total number of checkboxes that can be checked.
*/
add_filter( 'gform_field_content', function( $content, $field, $value, $entry_id, $form_id ) {
if( $field->is_form_editor() || $field->is_entry_detail() || ! gw_has_css_class( $field, 'gw-set-checked-as-read-only' ) ) {
return $content;
}
// Un-disable all inputs by default - AND - add onkeypress event. // Unset the hidden input that captures checked disabled checkboxes.
$onkeypress = 'var idBits = jQuery( this ).attr( "id" ).split( "_" ); jQuery( "#gwro_hidden_capture_' . $form_id . '_" + idBits[2] + "_" + idBits[3] ).val( this.checked ? this.value : "" ); console.log( this );';
$content = str_replace( "disabled='disabled'", " onkeypress='{$onkeypress}' onclick='{$onkeypress}'", $content );
$entry = GFFormsModel::get_current_lead();
if( ! $entry ) {
return $content;
}
$form = GFAPI::get_form( $form_id );
$gp_copy_cat = new GP_Copy_Cat();
$copy_fields = $gp_copy_cat->get_copy_cat_fields( $form );
$values = array();
// find the first source field that targets the current field and isn't hidden
foreach( $copy_fields as $group ) {
foreach( $group as $copy_field ) {
if ( intval( $copy_field['target'] ) != $field->id ) {
continue;
}
$source_field = GFFormsModel::get_field( $form, $copy_field['source'] );
if ( GFFormsModel::is_field_hidden( $form, $source_field, array() ) ) {
continue;
}
foreach ( $entry as $input_id => $value ) {
if ( intval( $input_id ) == $source_field->id ) {
$values[] = $value;
}
}
break 2;
}
}
$values = array_filter( $values );
if( empty( $values ) ) {
return $content;
}
// Disable checkboxes by value.
foreach( $values as $value ) {
$search = sprintf( "value='%s'", esc_attr( $value ) );
$replace = sprintf( "%s disabled='disabled'", $search );
$content = str_replace( $search, $replace, $content );
}
return $content;
}, 10, 5 );
if( ! function_exists( 'gw_has_css_class' ) ) {
function gw_has_css_class( $form_or_field, $class ) {
if( ! isset( $form_or_field['cssClass'] ) ) {
return false;
} else {
$classes = array_map( 'trim', explode( ' ', $form_or_field['cssClass'] ) );
return in_array( $class, $classes );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment