Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Created December 18, 2018 20:10
Show Gist options
  • Save zackkatz/105fc7f42bde0ac1e40f70a4caad5406 to your computer and use it in GitHub Desktop.
Save zackkatz/105fc7f42bde0ac1e40f70a4caad5406 to your computer and use it in GitHub Desktop.
GravityView - Modify the REST response sent by GravityView to use Gravity Forms field labels instead of just the field IDs
<?php
/**
* Modify the REST response sent by GravityView to use GF form field labels instead of field IDs
*/
add_filter( 'rest_pre_echo_response', function( array $result, WP_REST_Server $server, WP_REST_Request $request ) {
if ( empty( $result['entries'] ) ) {
return $result;
}
// Here's how to get the View information:
$view_id = $request->get_param( 'id' );
$View = \GV\View::by_id( $view_id );
// Gravity Forms form array
$form = $View->form->form;
foreach( (array) $result['entries'] as $key => $entry ) {
foreach ( $entry as $field_id => $value ) {
if ( ! is_numeric( $field_id ) ) {
continue;
}
$field = GVCommon::get_field( $form, floor( $field_id ) );
if ( ! $field || ! $field instanceof GF_Field ) {
continue;
}
$label = $field->get_field_label( true, $value );
$result['entries'][ $key ][ $label ] = $value;
unset( $result['entries'][ $key ][ $field_id ] );
}
}
return $result;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment