|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Gravity Connect // Airtable // Create Phone Number Relation |
4 |
| - * |
5 |
| - * Experimental Snippet 🧪 |
| 3 | + * Gravity Connect // Airtable // Create Relation |
6 | 4 | *
|
7 | 5 | * This snippet demonstrates how to create a relation between two tables in Airtable
|
8 | 6 | * when a GC Airtable feed is being processed. It uses an example assuming the following:
|
9 | 7 | *
|
10 |
| - * 1. There are a "People" table and "Phone Numbers" table in Airtable. |
11 |
| - * 2. The "Phone Numbers" table has, at minimum, a phone number field and a link field. |
12 |
| - * 3. There is a GCA feed that creates a record in the "People" table. |
13 |
| - * 4. The snippet adds a phone number to the "Phone Numbers" table and creates a relation |
14 |
| - * between the newly created "People" record and the "Phone Numbers" record. |
| 8 | + * 1. Theree is an Airtable Base with at least two tables. |
| 9 | + * 2. There is a Gravity Forms feed that is connected to one of the tables in the Airtable Base. |
| 10 | + * |
| 11 | + * TIP: you can easily find the following by creating a new GC Airtable feed, connecting |
| 12 | + * it to the Table which you want to create the relation in and then saving. |
| 13 | + * If you open the developer console in your browser and refresh the page, a table of all |
| 14 | + * the fields in the table will be logged. |
| 15 | + * |
| 16 | + * - $args['linked_table_id'] |
| 17 | + * - $args['link_field_id'] |
| 18 | + * - $args['value_mappings'] (the Airtable field IDs which you want to optionally add data to in the new linked record) |
15 | 19 | *
|
16 | 20 | * Installation:
|
17 | 21 | * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
|
|
22 | 26 | */
|
23 | 27 |
|
24 | 28 | /**
|
25 |
| - * You can also apply this to individual forms for feeds for more granular control. For example: |
26 |
| - * |
27 |
| - * add_action( 'gca_entry_added_to_airtable_FORMID', function( $entry, $create_record_resp, $gca_connection_instance ) {} ); |
28 |
| - * add_action( 'gca_entry_added_to_airtable_FORMID_FEEDID', function( $entry, $create_record_resp, $gca_connection_instance ) {} ); |
| 29 | + * @param mixed $args |
| 30 | + * @param? array $args['form_id'] The ID of the form to which this relation applies. |
| 31 | + * @param? array $args['feed_id'] The ID of the feed to which this relation applies. (Only used if form_id is also provided) |
| 32 | + * @param string $args['linked_table_id'] The ID of the linked table in Airtable. |
| 33 | + * @param string $args['link_field_id'] The ID of the field in the linked table that links to table connected to the feed. |
| 34 | + * @param? array $args['value_mappings'] An associative array mapping Airtable field IDs to Gravity Forms field IDs. |
29 | 35 | *
|
| 36 | + * @return void |
30 | 37 | */
|
31 |
| -add_action( 'gca_entry_added_to_airtable', function( $entry, $create_record_resp, $gca_connection_instance ) { |
32 |
| - $gf_phone_number_field_id = '1'; // The ID of the form field which contains the value you want to use to create the relation. |
33 |
| - $table_id = 'TODO'; // The ID of the Phone number |
34 |
| - $base_id = $gca_connection_instance->get_base_id(); |
| 38 | +function gca_create_relation( $args = array() ) { |
| 39 | + $args = wp_parse_args( |
| 40 | + $args, |
| 41 | + array( |
| 42 | + 'form_id' => null, // include form ID |
| 43 | + 'feed_id' => null, |
| 44 | + 'linked_table_id' => null, // The ID of the Phone Numbers table. |
| 45 | + 'link_field_id' => null, // The ID of the field in the Phone Numbers table that links to the People table. |
35 | 46 |
|
36 |
| - $phone_number = rgar( $entry, $gf_phone_number_field_id ); |
| 47 | + 'value_mappings' => array(), // The value mappings of Airtable field ids to Graivty Forms field ids. |
| 48 | + ) |
| 49 | + ); |
37 | 50 |
|
38 |
| - if ( empty( $phone_number ) ) { |
| 51 | + if ( ! $args['linked_table_id'] || ! $args['link_field_id'] ) { |
39 | 52 | return;
|
40 | 53 | }
|
41 | 54 |
|
42 |
| - /** |
43 |
| - * TIP: you can easily find the following by creating a new GC Airtable feed, connecting |
44 |
| - * it to the "Phone Numbers" table and saving. If you open the developer console in your |
45 |
| - * browser and refresh the page, a table of all the fields in the table will be logged. |
46 |
| - */ |
47 |
| - $phone_field_id = 'TODO'; // The ID of the phone number field in the Phone Numbers table. |
48 |
| - $link_field_id = 'TODO'; // The ID of the link field in the Phone Numbers table. |
| 55 | + $filter_name_pieces = array( 'gca_entry_added_to_airtable' ); |
49 | 56 |
|
50 |
| - $records = array( |
51 |
| - array( |
52 |
| - 'fields' => array( |
53 |
| - $phone_field_id => $phone_number, |
54 |
| - $link_field_id => array( $create_record_resp['id'] ), |
55 |
| - ), |
56 |
| - ), |
57 |
| - ); |
| 57 | + if ( $args['form_id'] ) { |
| 58 | + $filter_name_pieces[] = $args['form_id']; |
| 59 | + } |
58 | 60 |
|
59 |
| - try { |
60 |
| - $airtable_api = $gca_connection_instance->get_airtable_api(); |
61 |
| - $create_record_resp = $airtable_api->create_records( $base_id, $table_id, $records ); |
62 |
| - } catch ( Exception $e ) { |
63 |
| - $msg = gca_get_exception_message( $e ); |
64 |
| - gc_airtable()->log_error( $msg ); |
| 61 | + if ( $args['form_id'] && $args['feed_id'] ) { |
| 62 | + $filter_name_pieces[] = $args['feed_id']; |
65 | 63 | }
|
66 |
| -}, 10, 3 ); |
| 64 | + |
| 65 | + $filter_name = implode( '_', $filter_name_pieces ); |
| 66 | + |
| 67 | + add_action( |
| 68 | + $filter_name, |
| 69 | + function( $entry, $create_record_resp, $gca_connection_instance ) use ( $args ) { |
| 70 | + if ( empty( $args['linked_table_id'] ) ) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + $base_id = $gca_connection_instance->get_base_id(); |
| 75 | + |
| 76 | + $value_mappings = $args['value_mappings']; |
| 77 | + $mappings = array(); |
| 78 | + |
| 79 | + foreach ( $value_mappings as $airtable_field_id => $gf_field_id ) { |
| 80 | + $value = rgar( $entry, $gf_field_id ); |
| 81 | + |
| 82 | + if ( $value === '' || $value === null ) { |
| 83 | + // do not use empty() here so that the values 0 and 0.0 are allowed. |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + $mappings[ $airtable_field_id ] = $value; |
| 88 | + } |
| 89 | + |
| 90 | + $records = array( |
| 91 | + array( |
| 92 | + 'fields' => array_merge( |
| 93 | + array( |
| 94 | + $args['link_field_id'] => array( $create_record_resp['id'] ), |
| 95 | + ), |
| 96 | + $mappings |
| 97 | + ), |
| 98 | + ), |
| 99 | + ); |
| 100 | + |
| 101 | + try { |
| 102 | + $airtable_api = $gca_connection_instance->get_airtable_api(); |
| 103 | + $create_record_resp = $airtable_api->create_records( |
| 104 | + $base_id, |
| 105 | + $args['linked_table_id'], |
| 106 | + $records |
| 107 | + ); |
| 108 | + } catch ( Exception $e ) { |
| 109 | + $msg = gca_get_exception_message( $e ); |
| 110 | + gc_airtable()->log_error( $msg ); |
| 111 | + } |
| 112 | + }, |
| 113 | + 10, |
| 114 | + 3 |
| 115 | + ); |
| 116 | +} |
0 commit comments