Skip to content

gc-airtable/gca-create-relation.php: Added generic snippet demonstrating how to create an Airtable relation. #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions gc-airtable/gca-create-phone-number-relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this for now, but renaming. I might delete as well but am still undecided

/**
* Gravity Connect // Airtable // Create Phone Number Relation
*
* Experimental Snippet 🧪
*
* This snippet demonstrates how to create a relation between two tables in Airtable
* when a GC Airtable feed is being processed. It uses an example assuming the following:
*
* 1. There are a "People" table and "Phone Numbers" table in Airtable.
* 2. The "Phone Numbers" table has, at minimum, a phone number field and a link field.
* 3. There is a GCA feed that creates a record in the "People" table.
* 4. The snippet adds a phone number to the "Phone Numbers" table and creates a relation
* between the newly created "People" record and the "Phone Numbers" record.
*
* Installation:
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*
* References:
* * https://gravitywiz.com/documentation/gravity-connect-airtable
* * https://gravitywiz.com/documentation/gca_entry_added_to_airtable/
*/

/**
* You can also apply this to individual forms for feeds for more granular control. For example:
*
* add_action( 'gca_entry_added_to_airtable_FORMID', function( $entry, $create_record_resp, $gca_connection_instance ) {} );
* add_action( 'gca_entry_added_to_airtable_FORMID_FEEDID', function( $entry, $create_record_resp, $gca_connection_instance ) {} );
*
*/
add_action( 'gca_entry_added_to_airtable', function( $entry, $create_record_resp, $gca_connection_instance ) {
$gf_phone_number_field_id = '1'; // The ID of the form field which contains the value you want to use to create the relation.
$table_id = 'TODO'; // The ID of the Phone number
$base_id = $gca_connection_instance->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 );
162 changes: 122 additions & 40 deletions gc-airtable/gca-create-relation.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php
/**
* Gravity Connect // Airtable // Create Phone Number Relation
*
* Experimental Snippet 🧪
* Gravity Connect // Airtable // Create Relation
*
* This snippet demonstrates how to create a relation between two tables in Airtable
* when a GC Airtable feed is being processed. It uses an example assuming the following:
*
* 1. There are a "People" table and "Phone Numbers" table in Airtable.
* 2. The "Phone Numbers" table has, at minimum, a phone number field and a link field.
* 3. There is a GCA feed that creates a record in the "People" table.
* 4. The snippet adds a phone number to the "Phone Numbers" table and creates a relation
* between the newly created "People" record and the "Phone Numbers" record.
* 1. Theree is an Airtable Base with at least two tables.
* 2. There is a Gravity Forms feed that is connected to one of the tables in the Airtable Base.
*
* TIP: you can easily find the following by creating a new GC Airtable feed, connecting
* it to the Table which you want to create the relation in and then 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.
*
* - $args['linked_table_id']
* - $args['link_field_id']
* - $args['value_mappings'] (the Airtable field IDs which you want to optionally add data to in the new linked record)
*
* Installation:
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
Expand All @@ -22,45 +26,123 @@
*/

/**
* You can also apply this to individual forms for feeds for more granular control. For example:
*
* add_action( 'gca_entry_added_to_airtable_FORMID', function( $entry, $create_record_resp, $gca_connection_instance ) {} );
* add_action( 'gca_entry_added_to_airtable_FORMID_FEEDID', function( $entry, $create_record_resp, $gca_connection_instance ) {} );
* @param mixed $args
* @param? array $args['form_id'] The ID of the form to which this relation applies.
* @param? array $args['feed_id'] The ID of the feed to which this relation applies. (Only used if form_id is also provided)
* @param string $args['linked_table_id'] The ID of the linked table in Airtable.
* @param string $args['link_field_id'] The ID of the field in the linked table that links to table connected to the feed.
* @param? array $args['value_mappings'] An associative array mapping Airtable field IDs to Gravity Forms field IDs.
*
* @return void
*/
add_action( 'gca_entry_added_to_airtable', function( $entry, $create_record_resp, $gca_connection_instance ) {
$gf_phone_number_field_id = '1'; // The ID of the form field which contains the value you want to use to create the relation.
$table_id = 'TODO'; // The ID of the Phone number
$base_id = $gca_connection_instance->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
),
)
);
Loading