Skip to content

Commit b8c309d

Browse files
committed
gpeb-multi-field-sorting.php: Added snippet for multi field sorting on Entry Blocks.
1 parent 1956779 commit b8c309d

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

gp-entry-blocks/gpeb-multi-field-sorting.php

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,63 @@
33
* Gravity Perks // Entry Blocks // Multi Field Sorting
44
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
55
*
6-
* Use confirmation message from Form Settings when editing an entry in GPEB.
6+
* Mutli Field Sorting for GP Entry Blocks.
77
*
88
* Instruction Video: https://www.loom.com/share/b9867d2735d44519bf563961e9b30bd2
99
*/
10-
add_filter( 'gpeb_queryer_entries', function( $entries, $gf_queryer ) {
10+
class GPEB_Multi_Field_Sorting {
1111

12-
// Update the form ID to match your form.
13-
if ( $gf_queryer->form_id != 933 ) {
14-
return $entries;
12+
private $form_id;
13+
private $primary_sorting_field_id;
14+
private $secondary_sorting_field_id;
15+
private $sorting_direction;
16+
17+
public function __construct( $config = array() ) {
18+
$this->form_id = rgar( $config, 'form_id' );
19+
$this->primary_sorting_field_id = rgar( $config, 'primary_sorting_field_id' );
20+
$this->secondary_sorting_field_id = rgar( $config, 'secondary_sorting_field_id' );
21+
$this->sorting_direction = strtoupper( rgar( $config, 'sorting_direction', 'ASC' ) );
22+
23+
add_action( 'init', array( $this, 'init' ) );
24+
}
25+
26+
public function init() {
27+
add_filter( 'gpeb_queryer_entries', array( $this, 'sort_entries' ), 10, 2 );
28+
}
29+
30+
private function is_applicable_form( $current_form_id ) {
31+
return $current_form_id == $this->form_id;
1532
}
1633

17-
// Update the field IDs to match your form.
18-
$primary_sorting_field_id = '1.6';
19-
$secondary_sorting_field_id = '1.3';
20-
$sorting_direction = 'ASC'; // 'ASC' or 'DESC'
34+
public function sort_entries( $entries, $gf_queryer ) {
35+
if ( ! $this->is_applicable_form( $gf_queryer->form_id ) ) {
36+
return $entries;
37+
}
38+
39+
usort( $entries, array( $this, 'sort_callback' ) );
2140

22-
usort( $entries, function( $a, $b ) use (
23-
$primary_sorting_field_id,
24-
$secondary_sorting_field_id,
25-
$sorting_direction
26-
) {
27-
$a_primary = isset( $a[ $primary_sorting_field_id ] ) ? $a[ $primary_sorting_field_id ] : '';
28-
$b_primary = isset( $b[ $primary_sorting_field_id ] ) ? $b[ $primary_sorting_field_id ] : '';
41+
return $entries;
42+
}
43+
44+
private function sort_callback( $a, $b ) {
45+
$a_primary = rgar( $a, $this->primary_sorting_field_id, '' );
46+
$b_primary = rgar( $b, $this->primary_sorting_field_id, '' );
2947

3048
$cmp = strcasecmp( $a_primary, $b_primary );
31-
if ( $cmp === 0 ) {
32-
$a_secondary = isset( $a[ $secondary_sorting_field_id ] ) ? $a[ $secondary_sorting_field_id ] : '';
33-
$b_secondary = isset( $b[ $secondary_sorting_field_id ] ) ? $b[ $secondary_sorting_field_id ] : '';
49+
50+
if ( $cmp === 0 && $this->secondary_sorting_field_id ) {
51+
$a_secondary = rgar( $a, $this->secondary_sorting_field_id, '' );
52+
$b_secondary = rgar( $b, $this->secondary_sorting_field_id, '' );
3453
$cmp = strcasecmp( $a_secondary, $b_secondary );
3554
}
3655

37-
return ( $sorting_direction === 'DESC' ) ? -$cmp : $cmp;
38-
});
56+
return ( $this->sorting_direction === 'DESC' ) ? -$cmp : $cmp;
57+
}
58+
}
3959

40-
return $entries;
41-
}, 10, 2 );
60+
new GPEB_Multi_Field_Sorting( array(
61+
'form_id' => 933,
62+
'primary_sorting_field_id' => '1.6',
63+
'secondary_sorting_field_id' => '1.3',
64+
'sorting_direction' => 'ASC', // or 'DESC'
65+
) );

0 commit comments

Comments
 (0)