-
Notifications
You must be signed in to change notification settings - Fork 90
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
veryspry
wants to merge
1
commit into
master
Choose a base branch
from
matt/gca-relation-generic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* 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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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