Skip to content
Merged
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
87 changes: 87 additions & 0 deletions gc-notion/gcn-create-relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* Gravity Connect // Notion // Create Page Relation
* https://gravitywiz.com/documentation/gravity-connect-notion/
*
* Instructions:
*
* 1. Create a relation property in your Notion database that you want to populate with the selected page
* IDs. Take note of which database the relation property is configured to point to as it will be needed
* in the next step.
* 2. Populate a field in your form with one or more Notion Page ID's. These page IDs need to come from the
* the database in the previous step. The easiest way to do this is with the GP Populate Anything plugin,
* which allows you to populate a field with the IDs of Notion pages. This should work with any field
* whose value is will be a single string, for example a Dropdown, Choice, Single Line Text, or Hidden
* field, but isn't limited to those.
* 3. Configure the Usage Example below to match your Form, Feed, Field ID
* and Notion relation property name (this much match the relation property name in Notion exactly).
*/

function gcn_create_relation( $args = array() ) {
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
$field_id = isset( $args['field_id'] ) ? $args['field_id'] : null;
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;

Comment on lines +21 to +26
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add parameter validation for required arguments.

The function should validate that essential parameters are provided to prevent silent failures or unexpected behavior.

 function gcn_create_relation( $args = array() ) {
 	$form_id       = isset( $args['form_id'] ) ? $args['form_id'] : null;
 	$feed_id       = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
 	$field_id      = isset( $args['field_id'] ) ? $args['field_id'] : null;
 	$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
+
+	// Validate required parameters
+	if ( empty( $field_id ) || empty( $property_name ) ) {
+		return;
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function gcn_create_relation( $args = array() ) {
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
$field_id = isset( $args['field_id'] ) ? $args['field_id'] : null;
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
function gcn_create_relation( $args = array() ) {
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
$field_id = isset( $args['field_id'] ) ? $args['field_id'] : null;
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
// Validate required parameters
if ( empty( $field_id ) || empty( $property_name ) ) {
return;
}
🤖 Prompt for AI Agents
In gc-notion/gcn-create-relation.php around lines 21 to 26, the function
gcn_create_relation does not validate that required parameters like form_id,
feed_id, field_id, and property_name are provided. Add checks after extracting
these parameters to verify they are not null or empty, and handle missing
required arguments appropriately, such as returning an error or throwing an
exception, to prevent silent failures or unexpected behavior.

$filter_name_pieces = array( 'gcn_notion_page_data_add' );

if ( $form_id ) {
$filter_name_pieces[] = $form_id;
}

if ( $form_id && $feed_id ) {
$filter_name_pieces[] = $feed_id;
}

$filter_name = implode( '_', $filter_name_pieces );

add_filter(
$filter_name,
function ( $page_data, $form, $entry, $feed ) use ( $property_name, $field_id ) {
$page_id = rgar( $entry, $field_id );
$prop_type = 'relation';

if ( empty( $page_id ) ) {
return $page_data;
}

$page_data['properties'][ $property_name ] = array(
$prop_type => array(
array(
'id' => $page_id,
),
),
);

return $page_data;
}, 10, 4
);

}

/**
* Usage Example:
*/
gcn_create_relation(
array(
/**
* Change this to your form ID.
*/
'form_id' => 1,
/**
* Change this to the ID of the feed you want to use.
* You can technically omit this to apply to all feeds
* for the form, but it's recommended to specify it for clarity.
*/
'feed_id' => 2,
/**
* Change this to the ID of the field which holds the Notion Page ID.
*/
'field_id' => 3,
/**
* Change this to the name of the relation property in your Notion database.
*/
'property_name' => 'Tasks',
)
);
Loading