Skip to content

Commit ea9b93d

Browse files
authored
gpeb-filter-by-nested-field.php: Added snippet to allow filtering data via nested entries.
1 parent eb831ea commit ea9b93d

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
// check if required parameters are provided
18+
if ( empty( $config['parent_form_id'] ) || empty( $config['nested_form_id'] ) || empty( $config['parent_hidden_field_id'] ) || empty( $config['nested_target_field_id'] ) ) {
19+
return;
20+
}
21+
22+
$this->parent_form_id = rgar( $config, 'parent_form_id' );
23+
$this->nested_form_id = rgar( $config, 'nested_form_id' );
24+
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' );
25+
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' );
26+
27+
add_action( 'init', array( $this, 'init' ) );
28+
}
29+
30+
public function init() {
31+
add_filter( 'gpeb_filter_form', array( $this, 'modify_filter_form' ) );
32+
add_filter( 'gpeb_queryer_entries', array( $this, 'filter_entries_by_nested_value' ), 10, 2 );
33+
}
34+
35+
private function is_applicable_form( $form_id = null ) {
36+
if ( $form_id === null ) {
37+
$form_id = rgget( 'filters_form_id' );
38+
}
39+
return $form_id == $this->parent_form_id;
40+
}
41+
42+
public function modify_filter_form( $form ) {
43+
if ( ! $this->is_applicable_form( $form['id'] ) ) {
44+
return $form;
45+
}
46+
47+
foreach ( $form['fields'] as &$field ) {
48+
if ( $field->id == $this->parent_hidden_field_id ) {
49+
$nested_form_field = GFAPI::get_field( $this->nested_form_id, $this->nested_target_field_id );
50+
if ( ! $nested_form_field ) {
51+
error_log( sprintf( 'Nested form field with ID %d not found in form %d.', $this->nested_target_field_id, $this->nested_form_id ) );
52+
continue;
53+
}
54+
$field = $nested_form_field;
55+
$field->id = $this->parent_hidden_field_id;
56+
}
57+
}
58+
59+
return $form;
60+
}
61+
62+
public function filter_entries_by_nested_value( $entries, $gf_queryer ) {
63+
if ( ! $this->is_applicable_form() ) {
64+
return $entries;
65+
}
66+
67+
$filters = rgget( 'filters' );
68+
if ( ! isset( $filters[ $this->parent_hidden_field_id ] ) ) {
69+
return $entries;
70+
}
71+
72+
$nested_entries = GFAPI::get_entries( $this->nested_form_id, array(
73+
'field_filters' => array(
74+
array(
75+
'key' => $this->nested_target_field_id,
76+
'value' => $filters[ $this->parent_hidden_field_id ],
77+
),
78+
),
79+
) );
80+
81+
if ( is_wp_error( $nested_entries ) ) {
82+
error_log( sprintf( 'Error retrieving nested entries: %s', $nested_entries->get_error_message() ) );
83+
return $entries;
84+
}
85+
86+
$entries = array();
87+
$parent_entry_ids = array();
88+
89+
foreach ( $nested_entries as $nested_entry ) {
90+
$parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' );
91+
$entry = GFAPI::get_entry( $parent_entry_id );
92+
if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) {
93+
$parent_entry_ids[] = $parent_entry_id;
94+
$entries[] = $entry;
95+
}
96+
}
97+
98+
return $entries;
99+
}
100+
}
101+
102+
new GPEB_Filter_By_Nested_Entry( array(
103+
'parent_form_id' => 4,
104+
'nested_form_id' => 3,
105+
'parent_hidden_field_id' => 10,
106+
'nested_target_field_id' => 4,
107+
) );

0 commit comments

Comments
 (0)