Skip to content

Commit dc5b10d

Browse files
committed
gc-notion/gcn-create-relation.php: Added snippet to add a relation to an entry's Notion page.
1 parent e83c22b commit dc5b10d

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

gc-notion/gcn-create-relation.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* Gravity Connect // Notion // Create Page Relation
5+
* https://gravitywiz.com/documentation/gravity-connect-notion/
6+
*
7+
* Instructions:
8+
*
9+
* 1. Create a relation property in your Notion database that you want to populate with the selected page
10+
* IDs. Take note of which database the relation property is configured to point to as it will be needed
11+
* in the next step.
12+
* 2. Populate a field in your form with one or more Notion Page ID's. These page IDs need to come from the
13+
* the database in the previous step. The easiest way to do this is with the GP Populate Anything plugin,
14+
* which allows you to populate a field with the IDs of Notion pages. This should work with any field
15+
* whose value is will be a single string, for example a Dropdown, Choice, Single Line Text, or Hidden
16+
* field, but isn't limited to those.
17+
* 3. Configure the Usage Example below to match your Form, Feed, Field ID
18+
* and Notion relation property name (this much match the relation property name in Notion exactly).
19+
*/
20+
21+
function gcn_create_relation( $args = array() ) {
22+
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
23+
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
24+
$field_id = isset( $args['field_id'] ) ? $args['field_id'] : null;
25+
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
26+
27+
$filter_name_pieces = array( 'gcn_notion_page_data_add' );
28+
29+
if ( $form_id ) {
30+
$filter_name_pieces[] = $form_id;
31+
}
32+
33+
if ( $form_id && $feed_id ) {
34+
$filter_name_pieces[] = $feed_id;
35+
}
36+
37+
$filter_name = implode( '_', $filter_name_pieces );
38+
39+
add_filter(
40+
$filter_name,
41+
function ( $page_data, $form, $entry, $feed ) use ( $property_name, $field_id ) {
42+
$page_id = rgar( $entry, $field_id );
43+
$prop_type = 'relation';
44+
45+
if ( empty( $page_id ) ) {
46+
return $page_data;
47+
}
48+
49+
$page_data['properties'][ $property_name ] = array(
50+
$prop_type => array(
51+
array(
52+
'id' => $page_id,
53+
),
54+
),
55+
);
56+
57+
return $page_data;
58+
}, 10, 4
59+
);
60+
61+
}
62+
63+
/**
64+
* Usage Example:
65+
*/
66+
gcn_create_relation(
67+
array(
68+
/**
69+
* Change this to your form ID.
70+
*/
71+
'form_id' => 1,
72+
/**
73+
* Change this to the ID of the feed you want to use.
74+
* You can technically omit this to apply to all feeds
75+
* for the form, but it's recommended to specify it for clarity.
76+
*/
77+
'feed_id' => 2,
78+
/**
79+
* Change this to the ID of the field which holds the Notion Page ID.
80+
*/
81+
'field_id' => 3,
82+
/**
83+
* Change this to the name of the relation property in your Notion database.
84+
*/
85+
'property_name' => 'Tasks',
86+
)
87+
);

0 commit comments

Comments
 (0)