Exposure Hook - translate fields to meta-labels

I’ve developed a bit of code for translating fields, adding metadata labels, and flattening arrays like list objects in to proper JSON. I’ve written the code AS-IS, and you are free to use it as you see fit. Right now it triggers on OUTGOING webhook action in GravityFlow only… Feel free to adapt it and modify it.

The code is NOT complete, and does not cover “every field type” that GravityForms offers. It does however, include a recursive parsing loop for “parsing GravityWiz GravityPerks Nested Forms”…

I am sure there are other hooks in GravityFlow and GravityForms that you can use this with.

Hope you like this.

add_filter( 'gravityflow_webhook_args', 'gf_filter_gravityflow_webhook_args_subset', 10, 3 );
function gf_filter_gravityflow_webhook_args_subset( $args, $entry, $current_step ) {
	if ( empty( $current_step->body_type ) || $current_step->body_type == 'all_fields' ){
		$new = wwdvc_decode_form_entry( $entry );
		$args['body'] = json_encode( $new );
	}
	return $args;
}

function wwdvc_decode_form_entry( $entry ) {
	$form = GFAPI::get_form( $entry['form_id'] );

	$new = array();
	foreach ( $form['fields'] as $key => $field ) {
		//The entry values for checkbox (and other select fields) are not stored against their ID but ID.choice.
		//You should be able to add to the following array for other select based field types.
		if ( in_array( $field->type, array( 'checkbox' ) ) ) {
			foreach ( $field->choices as $choice_key => $choice ) {
				if ( isset( $entry[ $field->id . '.' . $choice_key ] ) && strlen( $entry[ $field->id . '.' . $choice_key ] ) > 0 ) {
					//Will create either "adminlabel.2" or "4.2" depending on the admin label being set
					if ( isset( $form['fields'][ $key ]->adminLabel ) && strlen( $form['fields'][ $key ]->adminLabel ) > 0 ) {
						$new[ $form['fields'][ $key ]->adminLabel . '.' . $choice_key ] = $entry[ $field->id . '.' . $choice_key ];
					} else {
						$new[ $field->id . '.' . $choice_key ] = $entry[ $field->id . '.' . $choice_key ];
					}
				}
			}
			//Assume it is a direct access item by entry field ID
		}
		else {
			$newkey = $field->id;
			$entry_val = $entry[ $field->id ];
				
			// if field type is LIST then unserialize the list to a proper array object in the VALUE
 			if ( in_array( $field->type, array( 'list' ) ) ) {
				$entry_val = maybe_unserialize( $entry_val );
			}

			if ( in_array( $field->type, array('section'))) {
				$newkey    = 'section.'.$field->id;
				$entry_val = $field['label'];
			} 
				
			if ( in_array( $field->type, array('consent'))) {
				$newkey    = 'consent';
				$entry_val = $entry[ $field->id . '.1'];
			}
				
			if ( in_array( $field->type, array('name'))) {
				$name_arr = array();
				foreach( $field->inputs as $row) {
					$name_arr[ $row['label'] ] = $entry[ $row[ 'id' ] ];
				};
				$entry_val = $name_arr;
			}
				
			if ( in_array( $field->type, array('form'))) {
				$nested_form_id = $field->gpnfForm;
				$new[ $form['fields'][ $key ]->adminLabel .'.nestedform-id' ] = $field->gpnfForm;
				$entry_val = explode( ',', $entry[ $field->id ] );
					
				$nested_arr = array();
				foreach($entry_val as $eid) {
					$nested_entry = GFAPI::get_entry( $eid );   // grab the nested entry
				    array_push($nested_arr, wwdvc_decode_form_entry( $nested_entry ));
				}
				$new[ $form['fields'][ $key ]->adminLabel .'.entries'] = $nested_arr;
			}
				
			if ( isset( $form['fields'][ $key ]->adminLabel ) && strlen( $form['fields'][ $key ]->adminLabel ) > 0 ) {
				$newkey = $form['fields'][ $key ]->adminLabel;
			}
			$new[ 'entry-id' ] = $entry[ 'id' ];
			$new[ $newkey ] = $entry_val;
		}
	}
	return $new;			
}
2 Likes

Thank you for sharing that. I’m sure there are Gravity Flow users out there looking for the same thing.

You are welcome. My hope?

  1. GravityFlow adds a checkbox [ x ] apply admin labels when sending data… either to webhooks, or to the API, etc…
  2. GravityForms adds a checkbox to actions or settings (ie: put the code in he core engine somewhere).
  3. GravityForms finishes the repeater fields
  4. someone out here extends the code to include more field types.

Thanks, hope folks can use this.
Dan

1 Like