Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save saifsultanc/6c505414fac80453ad86572cd485dc67 to your computer and use it in GitHub Desktop.

Select an option

Save saifsultanc/6c505414fac80453ad86572cd485dc67 to your computer and use it in GitHub Desktop.
gpro-enable-when-dynamically-populated.php
<?php
add_filter( 'gform_pre_render', function ( $form, $ajax, $field_values ) {
if ( ! session_id() ) {
session_start();
}
if ( ! isset( $_SESSION['gwreadonly_disabled_fields'] ) ) {
$_SESSION['gwreadonly_disabled_fields'] = array();
}
$gwreadonly_disabled_fields = $_SESSION['gwreadonly_disabled_fields'];
foreach ( $form['fields'] as &$field ) {
if ( ! $field->gwreadonly_enable ) {
continue;
}
$value = GFFormsModel::get_field_value( $field, $field_values, false );
if ( is_array( $value ) ) {
$value = array_filter( $value );
}
if ( ! $value ) {
$value = $field->gppa_hydrated_value;
if ( is_array( $value ) ) {
$value = array_filter( $value );
}
}
if ( $value && ! empty( $field->choices ) ) {
$has_matching_choice = false;
foreach ( $field->choices as $choice ) {
if ( $choice['value'] == $value ) {
$has_matching_choice = true;
break;
}
}
}
if ( isset( $gwreadonly_disabled_fields[ $field->id ] ) && $gwreadonly_disabled_fields[ $field->id ] ) {
$field->gwreadonly_enable = false;
continue;
}
if ( ! $value || ( isset( $has_matching_choice ) && ! $has_matching_choice ) ) {
$field->gwreadonly_enable = false;
$gwreadonly_disabled_fields[ $field->id ] = true;
}
}
$_SESSION['gwreadonly_disabled_fields'] = $gwreadonly_disabled_fields;
return $form;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment