Skip to content

Commit 008525f

Browse files
committed
gpeb-filter-by-nested-field.php: Added snippet to allow filtering data via nested entries.
1 parent 54c5969 commit 008525f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Entry Blocks // Filter values by Nested Entry.
4+
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
5+
*
6+
* Instruction Video: https://www.loom.com/share/f795e8b5ef58489794dca96e83fcd230
7+
*
8+
*/
9+
class GPEB_Filter_By_Nested_Entry {
10+
11+
private $parent_form_id;
12+
private $nested_form_id;
13+
private $parent_hidden_field_id;
14+
private $nested_target_field_id;
15+
16+
public function __construct( $config = array() ) {
17+
$this->parent_form_id = rgar( $config, 'parent_form_id' );
18+
$this->nested_form_id = rgar( $config, 'nested_form_id' );
19+
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' );
20+
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' );
21+
22+
add_action( 'init', array( $this, 'init' ) );
23+
}
24+
25+
public function init() {
26+
add_filter( 'gpeb_filter_form', array( $this, 'modify_filter_form' ) );
27+
add_filter( 'gpeb_queryer_entries', array( $this, 'filter_entries_by_nested_value' ), 10, 2 );
28+
}
29+
30+
private function is_applicable_form( $form_id = null ) {
31+
if ( $form_id === null ) {
32+
$form_id = rgget( 'filters_form_id' );
33+
}
34+
return $form_id == $this->parent_form_id;
35+
}
36+
37+
public function modify_filter_form( $form ) {
38+
if ( ! $this->is_applicable_form( $form['id'] ) ) {
39+
return $form;
40+
}
41+
42+
foreach ( $form['fields'] as &$field ) {
43+
if ( $field->id == $this->parent_hidden_field_id ) {
44+
$nested_form_field = GFAPI::get_field( $this->nested_form_id, $this->nested_target_field_id );
45+
$field = $nested_form_field;
46+
$field->id = $this->parent_hidden_field_id;
47+
}
48+
}
49+
50+
return $form;
51+
}
52+
53+
public function filter_entries_by_nested_value( $entries, $gf_queryer ) {
54+
if ( ! $this->is_applicable_form() ) {
55+
return $entries;
56+
}
57+
58+
$filters = rgget( 'filters' );
59+
if ( ! isset( $filters[ $this->parent_hidden_field_id ] ) ) {
60+
return $entries;
61+
}
62+
63+
$nested_entries = GFAPI::get_entries( $this->nested_form_id, array(
64+
'field_filters' => array(
65+
array(
66+
'key' => $this->nested_target_field_id,
67+
'value' => $filters[ $this->parent_hidden_field_id ],
68+
),
69+
),
70+
) );
71+
72+
$entries = array();
73+
$parent_entry_ids = array();
74+
75+
foreach ( $nested_entries as $nested_entry ) {
76+
$parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' );
77+
$entry = GFAPI::get_entry( $parent_entry_id );
78+
if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) {
79+
$parent_entry_ids[] = $parent_entry_id;
80+
$entries[] = $entry;
81+
}
82+
}
83+
84+
return $entries;
85+
}
86+
}
87+
88+
new GPEB_Filter_By_Nested_Entry( array(
89+
'parent_form_id' => 4,
90+
'nested_form_id' => 3,
91+
'parent_hidden_field_id' => 10,
92+
'nested_target_field_id' => 4,
93+
) );

0 commit comments

Comments
 (0)