Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active September 5, 2022 04:10
Show Gist options
  • Save zackkatz/cdac4cb87ee6e9816a33 to your computer and use it in GitHub Desktop.
Save zackkatz/cdac4cb87ee6e9816a33 to your computer and use it in GitHub Desktop.
GravityView - Hide Empty Fields in Edit Entry screen
<?php
/**
* Add filters to the edit entry inputs when GravityView starts to render an entry.
*/
add_action( 'gravityview/edit-entry/render/before', function() {
// GravityView methods are run at priority 10; by running at 20, this will run after GV.
add_filter( 'gform_field_content', 'gravityview_hide_empty_fields_in_edit_entry', 20, 5 );
} );
/**
* Remove the added filters.
*/
add_action( 'gravityview/edit-entry/render/after', function() {
remove_filter( 'gform_field_content', 'gravityview_hide_empty_fields_in_edit_entry', 20 );
} );
/**
* Modify the Edit Entry field HTML so that inputs aren't displayed for fields with empty values
*
* @param string $content The edit entry field
* @param array $field The Gravity Forms field array, with extra GravityView keys such as `gvCustomClass`
* @param string $value The value of the field
* @param int $entry_id The entry ID
* @param int $form_id The form ID
*
* @return string HTML output for the field in the Edit Entry form
*/
function gravityview_hide_empty_fields_in_edit_entry( $content = '', $field = array(), $value = '', $lead_id = 0, $form_id = 0 ) {
// This code makes the private method `get_field_value()` available publicly.
$reflection = new \ReflectionClass( 'GravityView_Edit_Entry_Render' );
$method = $reflection->getMethod( 'get_field_value' );
$method->setAccessible( true );
$value = $method->invokeArgs( GravityView_Edit_Entry::getInstance()->instances['render'], array( $field ) );
// If the value is empty, return nothing.
if ( '' === $value || is_array( $value ) && ! array_filter( $value ) ) {
return '';
}
// Otherwise, return the original HTML output
return $content;
}
@jasimp
Copy link

jasimp commented Sep 5, 2022

how to exclude some specific empty fields from hiding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment