From 7cc03a0cb7b33b2fd3f1b0b18f89b16fce26aa2e Mon Sep 17 00:00:00 2001 From: veryspry Date: Mon, 9 Jun 2025 11:15:43 +0200 Subject: [PATCH] `gc-airtable/gca-create-relation.php`: Added generic snippet demonstrating how to create an Airtable relation. --- .../gca-create-phone-number-relation.php | 66 +++++++ gc-airtable/gca-create-relation.php | 162 +++++++++++++----- 2 files changed, 188 insertions(+), 40 deletions(-) create mode 100644 gc-airtable/gca-create-phone-number-relation.php diff --git a/gc-airtable/gca-create-phone-number-relation.php b/gc-airtable/gca-create-phone-number-relation.php new file mode 100644 index 000000000..26f52dcfc --- /dev/null +++ b/gc-airtable/gca-create-phone-number-relation.php @@ -0,0 +1,66 @@ +get_base_id(); + + $phone_number = rgar( $entry, $gf_phone_number_field_id ); + + if ( empty( $phone_number ) ) { + return; + } + + /** + * TIP: you can easily find the following by creating a new GC Airtable feed, connecting + * it to the "Phone Numbers" table and saving. If you open the developer console in your + * browser and refresh the page, a table of all the fields in the table will be logged. + */ + $phone_field_id = 'TODO'; // The ID of the phone number field in the Phone Numbers table. + $link_field_id = 'TODO'; // The ID of the link field in the Phone Numbers table. + + $records = array( + array( + 'fields' => array( + $phone_field_id => $phone_number, + $link_field_id => array( $create_record_resp['id'] ), + ), + ), + ); + + try { + $airtable_api = $gca_connection_instance->get_airtable_api(); + $create_record_resp = $airtable_api->create_records( $base_id, $table_id, $records ); + } catch ( Exception $e ) { + $msg = gca_get_exception_message( $e ); + gc_airtable()->log_error( $msg ); + } +}, 10, 3 ); diff --git a/gc-airtable/gca-create-relation.php b/gc-airtable/gca-create-relation.php index 26f52dcfc..a1e83bd15 100644 --- a/gc-airtable/gca-create-relation.php +++ b/gc-airtable/gca-create-relation.php @@ -1,17 +1,21 @@ get_base_id(); +function gca_create_relation( $args = array() ) { + $args = wp_parse_args( + $args, + array( + 'form_id' => null, // include form ID + 'feed_id' => null, + 'linked_table_id' => null, // The ID of the Phone Numbers table. + 'link_field_id' => null, // The ID of the field in the Phone Numbers table that links to the People table. - $phone_number = rgar( $entry, $gf_phone_number_field_id ); + 'value_mappings' => array(), // The value mappings of Airtable field ids to Graivty Forms field ids. + ) + ); - if ( empty( $phone_number ) ) { + if ( ! $args['linked_table_id'] || ! $args['link_field_id'] ) { return; } - /** - * TIP: you can easily find the following by creating a new GC Airtable feed, connecting - * it to the "Phone Numbers" table and saving. If you open the developer console in your - * browser and refresh the page, a table of all the fields in the table will be logged. - */ - $phone_field_id = 'TODO'; // The ID of the phone number field in the Phone Numbers table. - $link_field_id = 'TODO'; // The ID of the link field in the Phone Numbers table. + $filter_name_pieces = array( 'gca_entry_added_to_airtable' ); - $records = array( - array( - 'fields' => array( - $phone_field_id => $phone_number, - $link_field_id => array( $create_record_resp['id'] ), - ), - ), - ); + if ( $args['form_id'] ) { + $filter_name_pieces[] = $args['form_id']; + } - try { - $airtable_api = $gca_connection_instance->get_airtable_api(); - $create_record_resp = $airtable_api->create_records( $base_id, $table_id, $records ); - } catch ( Exception $e ) { - $msg = gca_get_exception_message( $e ); - gc_airtable()->log_error( $msg ); + if ( $args['form_id'] && $args['feed_id'] ) { + $filter_name_pieces[] = $args['feed_id']; } -}, 10, 3 ); + + $filter_name = implode( '_', $filter_name_pieces ); + + add_action( + $filter_name, + function( $entry, $create_record_resp, $gca_connection_instance ) use ( $args ) { + if ( empty( $args['linked_table_id'] ) ) { + return; + } + + $base_id = $gca_connection_instance->get_base_id(); + + $value_mappings = $args['value_mappings']; + $mappings = array(); + + foreach ( $value_mappings as $airtable_field_id => $gf_field_id ) { + $value = rgar( $entry, $gf_field_id ); + + if ( $value === '' || $value === null ) { + // do not use empty() here so that the values 0 and 0.0 are allowed. + continue; + } + + $mappings[ $airtable_field_id ] = $value; + } + + $records = array( + array( + 'fields' => array_merge( + array( + $args['link_field_id'] => array( $create_record_resp['id'] ), + ), + $mappings + ), + ), + ); + + try { + $airtable_api = $gca_connection_instance->get_airtable_api(); + $create_record_resp = $airtable_api->create_records( + $base_id, + $args['linked_table_id'], + $records + ); + } catch ( Exception $e ) { + $msg = gca_get_exception_message( $e ); + gc_airtable()->log_error( $msg ); + } + }, + 10, + 3 + ); +} + +/** + * Usage Example: + */ +gca_create_relation( + array( + /** + * Change this to your form ID. + */ + 'form_id' => 1, + /** + * Change this to the ID of the feed you want to use. + */ + 'feed_id' => 2, + /** + * Change this to the ID of the linked table in Airtable. + */ + 'linked_table_id' => 'tblXXXXXXXXXXXXXX', + /** + * Change this to the ID of the link field in Airtable. + */ + 'link_field_id' => 'fldXXXXXXXXXXXXXX', + /** + * Change this to an array of value mappings. + * The keys are Airtable field IDs and the values are Gravity Forms field IDs. + */ + 'value_mappings' => array( + 'fldXXXXXXXXXXXXXX' => 3, // map Airtable field "fldXXXXXXXXXXXXXX" to Gravity Forms field with ID 3 + // Add more mappings as needed + ), + ) +);