|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Advanced Select // Enable Advanced Select for Date & Time Fields |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-advanced-select/ |
| 5 | + * |
| 6 | + * Enable Advanced Select for Date and Time Fields |
| 7 | + * |
| 8 | + * Instruction Video: https://www.loom.com/share/6a681f81df3f4043a85aed7e8c38bc1f |
| 9 | + * |
| 10 | + * Instructions: |
| 11 | + * |
| 12 | + * 1. Install this snippet by following the instructions here: |
| 13 | + * https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 14 | + */ |
| 15 | + |
| 16 | +add_filter( 'gpadvs_is_supported_input_type', function ( $supported_types ) { |
| 17 | + array_push( $supported_types, 'date', 'time' ); |
| 18 | + |
| 19 | + return $supported_types; |
| 20 | +}, 10, 1 ); |
| 21 | + |
| 22 | +add_filter( 'gpadvs_js_init_args', function ( $init_args, $form, $field ) { |
| 23 | + // For Date Dropdown, it must be enabled on all of the MM / DD / YYYY |
| 24 | + if ( $field->type == 'date' && $field->dateType == 'datedropdown' && $field->gpadvsEnable ) { |
| 25 | + // Initialize script for each part: Month (_1), Day (_2), Year (_3) |
| 26 | + foreach ( range( 1, 3 ) as $index ) { |
| 27 | + $init_args_for_date_dropdown = $init_args; |
| 28 | + $init_args_for_date_dropdown['fieldId'] = $init_args['fieldId'] . '_' . $index; |
| 29 | + |
| 30 | + $script = 'new GPAdvancedSelect(' . json_encode( $init_args_for_date_dropdown ) . ');'; |
| 31 | + $slug = 'gp_advanced_select_' . $init_args_for_date_dropdown['formId'] . '_' . $init_args_for_date_dropdown['fieldId']; |
| 32 | + |
| 33 | + // Add the script for this Date part |
| 34 | + GFFormDisplay::add_init_script( $init_args_for_date_dropdown['formId'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); |
| 35 | + } |
| 36 | + // ignore the default script |
| 37 | + return array(); |
| 38 | + } |
| 39 | + |
| 40 | + // Time Field Only has dropdown on the AM/PM. |
| 41 | + if ( $field->type == 'time' && $field->gpadvsEnable ) { |
| 42 | + $init_args['fieldId'] = $init_args['fieldId'] . '_3'; |
| 43 | + } |
| 44 | + |
| 45 | + return $init_args; |
| 46 | +}, 10, 3 ); |
0 commit comments