<?php
/**
* Gravity Perks // Read Only // Enable When Dynamically Populated
* https://gravitywiz.com/documentation/gravity-forms-read-only/
*
* Use this snippet to make fields readonly if they are dynamically populated. You must enable the "Read-only" field
* setting for this functionality to apply.
*
* See video: https://www.loom.com/share/7c42fc8c0aef42d990ae600675546774
*/
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 we have a value and we're populating a choice-based field, make sure the value matches a choice.
if ( $value && ! empty( $field->choices ) ) {
$has_matching_choice = false;
foreach ( $field->choices as $choice ) {
if ( $choice['value'] == $value ) {
$has_matching_choice = true;
break;
}
}
}
// Multi page form support.
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 );