|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Gravity Connect // Notion // Add Relation Property From Dropdown |
| 5 | + * https://gravitywiz.com/documentation/gravity-connect-notion/ |
| 6 | + * |
| 7 | + * Instructions: |
| 8 | + * |
| 9 | + * 1. Using GP Populate Anything, populate a Dropdown field with the IDs of Notion pages you want to relate to. |
| 10 | + * 2. Create a Relation property in your Notion database that you want to populate with the selected page IDs. |
| 11 | + * 3. Configure the Usage Example below to match your form, feed, dropdown field id and relation property name (this much match the relation property name in Notion exactly). |
| 12 | + */ |
| 13 | + |
| 14 | +function gcn_add_relation_property_from_dropdown( $args = array() ) { |
| 15 | + $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; |
| 16 | + $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; |
| 17 | + $dropdown_field_id = isset( $args['dropdown_field_id'] ) ? $args['dropdown_field_id'] : null; |
| 18 | + $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; |
| 19 | + |
| 20 | + $filter_name_pieces = array( 'gcn_notion_page_data_add' ); |
| 21 | + |
| 22 | + if ( $form_id ) { |
| 23 | + $filter_name_pieces[] = $form_id; |
| 24 | + } |
| 25 | + |
| 26 | + if ( $form_id && $feed_id ) { |
| 27 | + $filter_name_pieces[] = $feed_id; |
| 28 | + } |
| 29 | + |
| 30 | + $filter_name = implode( '_', $filter_name_pieces ); |
| 31 | + |
| 32 | + add_filter( |
| 33 | + $filter_name, |
| 34 | + function ( $page_data, $form, $entry, $feed ) use ( $property_name, $dropdown_field_id ) { |
| 35 | + $page_id = rgar( $entry, $dropdown_field_id ); |
| 36 | + $prop_type = 'relation'; |
| 37 | + |
| 38 | + if ( empty( $page_id ) ) { |
| 39 | + return $page_data; |
| 40 | + } |
| 41 | + |
| 42 | + $page_data['properties'][ $property_name ] = array( |
| 43 | + $prop_type => array( |
| 44 | + array( |
| 45 | + 'id' => $page_id, |
| 46 | + ), |
| 47 | + ), |
| 48 | + ); |
| 49 | + |
| 50 | + return $page_data; |
| 51 | + }, 10, 4 |
| 52 | + ); |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Usage Example: |
| 58 | + */ |
| 59 | +gcn_add_relation_property_from_dropdown( |
| 60 | + array( |
| 61 | + 'form_id' => 5, // Change this to your form ID. |
| 62 | + 'feed_id' => 2, // Change this to the ID of the feed you want to use. |
| 63 | + 'dropdown_field_id' => 3, // Change this to the ID of the field you want to populate. |
| 64 | + 'property_name' => 'Tasks', // Change this to the name of the relation property in your Notion database. |
| 65 | + ) |
| 66 | +); |
0 commit comments