|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Connect // Notion // Radio Button to Date Property Mapping |
| 4 | + * |
| 5 | + * This snippet demonstrates how to map a radio button field to a Date property in Notion. |
| 6 | + * |
| 7 | + * Instructions: |
| 8 | + * 1. Modify the filter name to scope as needed. |
| 9 | + * * see filter reference for full list of variants: https://gravitywiz.com/documentation/gcn_notion_page_data/) |
| 10 | + * |
| 11 | + * 2. Get the Date property ID from the Notion database and update the $property_id variable. |
| 12 | + * * Enable the gcn_show_feed_database_debug_info hook: https://gravitywiz.com/documentation/gcn_show_feed_database_debug_info/ |
| 13 | + * * Open the browser developer console and navigate to a GC Notion feed which is connected to the database you are populating into. |
| 14 | + * * Scroll up in the developer console until you see the debug info displayed. |
| 15 | + * * Find the date property and copy the ID. |
| 16 | + * 3. Double check that the date format in the radio field options matches the $date_format variable. |
| 17 | + * * Note: if it doesn't, you can either adjust the date format in the radio field options or adjust the $date_format variable. |
| 18 | + * |
| 19 | + * Installation: |
| 20 | + * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 21 | + * |
| 22 | + * References: |
| 23 | + * * GC Notion: https://gravitywiz.com/documentation/gravity-connect-notion/ |
| 24 | + * * Notion page POST reference: https://developers.notion.com/reference/post-page |
| 25 | + * * Notion Database property reference: https://developers.notion.com/reference/page-property-values#date |
| 26 | + * |
| 27 | + * @since 1.0-beta-1.9 |
| 28 | + */ |
| 29 | + |
| 30 | +add_filter( 'gcn_notion_page_data', function( $page_data, $form, $entry, $feed ) { |
| 31 | + $radio_date_field_id = 4; |
| 32 | + $property_id = 'BpFI'; |
| 33 | + |
| 34 | + // date format in radio field value expected to be: 'd F Y'. E.g. 01 January 2025 |
| 35 | + $date_format = 'd F Y'; |
| 36 | + $radio_field_value = $entry[ $radio_date_field_id ]; |
| 37 | + $date = \DateTime::createFromFormat( $date_format, $radio_field_value ); |
| 38 | + $iso_date = $date ? $date->format( 'Y-m-d' ) : ''; |
| 39 | + |
| 40 | + $page_data['properties'][ $property_id ] = array( |
| 41 | + 'date' => array( |
| 42 | + 'start' => $iso_date, |
| 43 | + ), |
| 44 | + ); |
| 45 | + |
| 46 | + return $page_data; |
| 47 | +}, 10, 4 ); |
0 commit comments