|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Nested Forms // Sort Nested Form Entries |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 5 | + * |
| 6 | + * Instruction Video: https://www.loom.com/share/c9fd421f33dd4925ae2c0139abace5f0 |
| 7 | + * |
| 8 | + * This snippet allows you to sort the entries of a Nested Form field based on a specific field in the child form. |
| 9 | + * |
| 10 | + * Plugin Name: GP Nested Forms - Sort Nested Form Entries |
| 11 | + * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 12 | + * Description: Enable sorting for Nested Form entries. |
| 13 | + * Author: Gravity Wiz |
| 14 | + * Version: 0.1 |
| 15 | + * Author URI: https://gravitywiz.com/ |
| 16 | + */ |
| 17 | +class GPNF_Sort_Nested_Entries { |
| 18 | + |
| 19 | + private $_args = array(); |
| 20 | + |
| 21 | + public function __construct( $args = array() ) { |
| 22 | + $this->_args = wp_parse_args( $args, array( |
| 23 | + 'parent_form_id' => false, |
| 24 | + 'nested_field_id' => false, |
| 25 | + 'sort_field_id' => false, |
| 26 | + 'sort_order' => 'asc', |
| 27 | + ) ); |
| 28 | + |
| 29 | + add_action( 'init', array( $this, 'init' ) ); |
| 30 | + } |
| 31 | + |
| 32 | + public function init() { |
| 33 | + add_filter( "gpnf_template_args_{$this->_args['parent_form_id']}_{$this->_args['nested_field_id']}", array( $this, 'sort_entries_php' ) ); |
| 34 | + add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); |
| 35 | + } |
| 36 | + |
| 37 | + public function sort_entries_php( $args ) { |
| 38 | + $field_id = $this->_args['sort_field_id']; |
| 39 | + $order = strtolower( $this->_args['sort_order'] ); |
| 40 | + |
| 41 | + if ( isset( $args['entries'] ) ) { |
| 42 | + usort( $args['entries'], function( $a, $b ) use ( $field_id, $order ) { |
| 43 | + $first = rgar( $a, $field_id ); |
| 44 | + $second = rgar( $b, $field_id ); |
| 45 | + |
| 46 | + if ( $first == $second ) { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + if ( $order === 'asc' ) { |
| 51 | + return ( $first < $second ) ? -1 : 1; |
| 52 | + } else { |
| 53 | + return ( $first > $second ) ? -1 : 1; |
| 54 | + } |
| 55 | + } ); |
| 56 | + } |
| 57 | + |
| 58 | + return $args; |
| 59 | + } |
| 60 | + |
| 61 | + public function load_form_script( $form, $is_ajax_enabled ) { |
| 62 | + if ( $form['id'] == $this->_args['parent_form_id'] && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { |
| 63 | + add_action( 'wp_footer', array( $this, 'output_script' ) ); |
| 64 | + add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); |
| 65 | + } |
| 66 | + |
| 67 | + return $form; |
| 68 | + } |
| 69 | + |
| 70 | + public function output_script() { |
| 71 | + $args = array( |
| 72 | + 'parentFormId' => (int) $this->_args['parent_form_id'], |
| 73 | + 'nestedFieldId' => (int) $this->_args['nested_field_id'], |
| 74 | + 'sortFieldId' => (int) $this->_args['sort_field_id'], |
| 75 | + 'sortOrder' => strtolower( $this->_args['sort_order'] ), |
| 76 | + ); |
| 77 | + ?> |
| 78 | + |
| 79 | + <script type="text/javascript"> |
| 80 | + (function($) { |
| 81 | + const sortFieldId = "<?php echo esc_js( $args['sortFieldId'] ); ?>"; |
| 82 | + const sortOrder = "<?php echo esc_js( $args['sortOrder'] ); ?>"; |
| 83 | + |
| 84 | + window.gform.addFilter('gpnf_sorted_entries', function(entries, formId, fieldId, gpnf) { |
| 85 | + if (!entries || !entries.length || !entries[0][sortFieldId]) { |
| 86 | + console.warn(`GPNF Sort: Field ID ${sortFieldId} not found in entries or entries are empty.`); |
| 87 | + return entries; |
| 88 | + } |
| 89 | + |
| 90 | + return entries.sort((a, b) => { |
| 91 | + let valA = a[sortFieldId]?.label || ''; |
| 92 | + let valB = b[sortFieldId]?.label || ''; |
| 93 | + |
| 94 | + if (sortOrder === 'desc') { |
| 95 | + return valB.localeCompare(valA); |
| 96 | + } |
| 97 | + |
| 98 | + return valA.localeCompare(valB); |
| 99 | + }); |
| 100 | + }); |
| 101 | + })(jQuery); |
| 102 | + </script> |
| 103 | + |
| 104 | + <?php |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +# Example usage: |
| 109 | +new GPNF_Sort_Nested_Entries( array( |
| 110 | + 'parent_form_id' => 184, |
| 111 | + 'nested_field_id' => 1, |
| 112 | + 'sort_field_id' => 2, // field on child form to sort by |
| 113 | + 'sort_order' => 'asc', // or 'desc' |
| 114 | +) ); |
0 commit comments