-
Notifications
You must be signed in to change notification settings - Fork 92
gpeb-filter-by-nested-field.php: Added snippet to allow filtering data via nested entries.
#1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
| /** | ||
| * Gravity Perks // Entry Blocks // Filter values by Nested Entry. | ||
| * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/ | ||
| * | ||
| * Instruction Video: https://www.loom.com/share/f795e8b5ef58489794dca96e83fcd230 | ||
| * | ||
| */ | ||
| class GPEB_Filter_By_Nested_Entry { | ||
|
|
||
| private $parent_form_id; | ||
| private $nested_form_id; | ||
| private $parent_hidden_field_id; | ||
| private $nested_target_field_id; | ||
|
|
||
| public function __construct( $config = array() ) { | ||
| // check if required parameters are provided | ||
| if ( empty( $config['parent_form_id'] ) || empty( $config['nested_form_id'] ) || empty( $config['parent_hidden_field_id'] ) || empty( $config['nested_target_field_id'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $this->parent_form_id = rgar( $config, 'parent_form_id' ); | ||
| $this->nested_form_id = rgar( $config, 'nested_form_id' ); | ||
| $this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); | ||
| $this->nested_target_field_id = rgar( $config, 'nested_target_field_id' ); | ||
|
|
||
| add_action( 'init', array( $this, 'init' ) ); | ||
| } | ||
|
|
||
| public function init() { | ||
| add_filter( 'gpeb_filter_form', array( $this, 'modify_filter_form' ) ); | ||
| add_filter( 'gpeb_queryer_entries', array( $this, 'filter_entries_by_nested_value' ), 10, 2 ); | ||
| } | ||
|
|
||
| private function is_applicable_form( $form_id = null ) { | ||
| if ( $form_id === null ) { | ||
| $form_id = rgget( 'filters_form_id' ); | ||
| } | ||
| return $form_id == $this->parent_form_id; | ||
| } | ||
|
|
||
| public function modify_filter_form( $form ) { | ||
| if ( ! $this->is_applicable_form( $form['id'] ) ) { | ||
| return $form; | ||
| } | ||
|
|
||
| foreach ( $form['fields'] as &$field ) { | ||
| if ( $field->id == $this->parent_hidden_field_id ) { | ||
| $nested_form_field = GFAPI::get_field( $this->nested_form_id, $this->nested_target_field_id ); | ||
| if ( ! $nested_form_field ) { | ||
| error_log( sprintf( 'Nested form field with ID %d not found in form %d.', $this->nested_target_field_id, $this->nested_form_id ) ); | ||
| continue; | ||
| } | ||
| $field = $nested_form_field; | ||
| $field->id = $this->parent_hidden_field_id; | ||
| } | ||
| } | ||
|
|
||
| return $form; | ||
| } | ||
|
|
||
| public function filter_entries_by_nested_value( $entries, $gf_queryer ) { | ||
| if ( ! $this->is_applicable_form() ) { | ||
| return $entries; | ||
| } | ||
|
|
||
| $filters = rgget( 'filters' ); | ||
| if ( ! isset( $filters[ $this->parent_hidden_field_id ] ) ) { | ||
| return $entries; | ||
| } | ||
|
|
||
| $nested_entries = GFAPI::get_entries( $this->nested_form_id, array( | ||
| 'field_filters' => array( | ||
| array( | ||
| 'key' => $this->nested_target_field_id, | ||
| 'value' => $filters[ $this->parent_hidden_field_id ], | ||
| ), | ||
| ), | ||
| ) ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if ( is_wp_error( $nested_entries ) ) { | ||
| error_log( sprintf( 'Error retrieving nested entries: %s', $nested_entries->get_error_message() ) ); | ||
| return $entries; | ||
| } | ||
|
|
||
| $entries = array(); | ||
| $parent_entry_ids = array(); | ||
|
|
||
| foreach ( $nested_entries as $nested_entry ) { | ||
| $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' ); | ||
| $entry = GFAPI::get_entry( $parent_entry_id ); | ||
| if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) { | ||
| $parent_entry_ids[] = $parent_entry_id; | ||
| $entries[] = $entry; | ||
| } | ||
| } | ||
|
|
||
| return $entries; | ||
| } | ||
| } | ||
|
|
||
| new GPEB_Filter_By_Nested_Entry( array( | ||
| 'parent_form_id' => 4, | ||
| 'nested_form_id' => 3, | ||
| 'parent_hidden_field_id' => 10, | ||
| 'nested_target_field_id' => 4, | ||
| ) ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saifsultanc do you think this could work without the need for a hidden field by simply adding a copy of the nested form field here to the parent
$form['fields']array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reservations for that were:
Keeping a separate "placeholder" like hidden field seemed a little safer and cleaner to me. What you say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saifsultanc Good points! Let's go hidden field 🤝