|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // GP Read Only // Set Fields as Readonly On Edit |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-read-only/ |
| 5 | + * |
| 6 | + * Configure fields to be readonly when editing via Entry Blocks or GravityView or Gravity Flow User Input Step. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * |
| 10 | + * 1. Install this code as a plugin or as a snippet. |
| 11 | + * 2. Add the `gpro-readonly-on-edit` CSS Class Name to field's Custom CSS Class setting. |
| 12 | + * |
| 13 | + * Plugin Name: GP Read Only — Set Fields as Readonly On Edit |
| 14 | + * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-read-only/ |
| 15 | + * Description: This snippet allows you to set read only for fields when editing. |
| 16 | + * Author: Gravity Wiz |
| 17 | + * Version: 0.1 |
| 18 | + * Author URI: https://gravitywiz.com/ |
| 19 | + */ |
| 20 | + |
| 21 | +add_filter( 'gform_pre_render', 'gpeb_set_readonly_on_edit' ); |
| 22 | +add_filter( 'gform_pre_process', 'gpeb_set_readonly_on_edit' ); |
| 23 | + |
| 24 | +function gpeb_set_readonly_on_edit( $form ) { |
| 25 | + |
| 26 | + $is_block = (bool) rgpost( 'gpeb_entry_id' ); |
| 27 | + if ( ! $is_block ) { |
| 28 | + $is_block = class_exists( 'WP_Block_Supports' ) && rgar( WP_Block_Supports::$block_to_render, 'blockName' ) === 'gp-entry-blocks/edit-form'; |
| 29 | + } |
| 30 | + |
| 31 | + $is_gravityview = function_exists( 'gravityview' ) && gravityview()->request->is_edit_entry(); |
| 32 | + $is_gravity_flow = rgget( 'lid' ) && rgget( 'page' ) == 'gravityflow-inbox'; |
| 33 | + |
| 34 | + // disable the target field for GPEB, GravityView and Gravity Flow User Input step. |
| 35 | + if ( $is_block || $is_gravityview || $is_gravity_flow ) { |
| 36 | + foreach ( $form['fields'] as &$field ) { |
| 37 | + if ( strpos( $field->cssClass, 'gpro-readonly-on-edit' ) !== false ) { |
| 38 | + $field->gwreadonly_enable = true; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return $form; |
| 44 | +} |
0 commit comments