-
Notifications
You must be signed in to change notification settings - Fork 92
gpnf-gpeb-auto-attach-child-entries.php: Added snippet to Auto-attach Child Entries to Parent when Editing via GP Entry Blocks.
#1145
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
Conversation
…ch Child Entries to Parent when Editing via GP Entry Blocks.
WalkthroughIntroduces a PHP snippet that hooks into gpnf_set_parent_entry_id to set the Nested Forms child entry’s parent ID from the edit_entry request parameter when editing via GP Entry Blocks. If edit_entry is present, it overrides the parent ID; otherwise, the original ID passes through. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant GP Entry Blocks
participant GPNF
participant Snippet
User->>GP Entry Blocks: Submit child form (with edit_entry)
GP Entry Blocks->>GPNF: Process child entry
GPNF->>Snippet: gpnf_set_parent_entry_id(parentId)
Snippet->>Snippet: Check rgget('edit_entry')
alt edit_entry present
Snippet-->>GPNF: Return edit_entry as parentId
else
Snippet-->>GPNF: Return original parentId
end
GPNF->>GP Entry Blocks: Attach child to parent entry
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Possibly related PRs
Suggested reviewers
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php (2)
12-17: Consider setting an explicit filter priority to ensure final overrideIf other code also filters gpnf_set_parent_entry_id, bumping priority slightly helps this run after defaults.
Apply this diff:
-} ); +}, 20 );
2-11: Docblock: call out Entry Blocks requirement and docs linkMinor clarity improvement for future readers scanning the snippet.
Apply this diff:
/** * Gravity Perks // Nested Forms // Auto-attach Child Entries to Parent when Editing via GP Entry Blocks. * https://gravitywiz.com/documentation/gravity-forms-nested-forms/ * * By default, when adding a new child entry to a Nested Form field while editing a parent entry via GP Entry Blocks, the * child entry is saved to the session and will not be attached to the parent entry unless you click "Update" on the * parent entry form. * * Use this snippet to automatically attach the child entry to the parent as soon as the child form is submitted. + * Requires: Gravity Perks Entry Blocks (GP Entry Blocks). + * Docs: https://gravitywiz.com/documentation/gravity-forms-entry-blocks/ */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php(1 hunks)
🔇 Additional comments (1)
gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php (1)
1-17: LGTM functionally — minimal snippet achieves the intended behaviorThe approach mirrors existing patterns in the library and should attach child entries immediately during Entry Blocks edits.
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | ||
| if ( rgget( 'edit_entry' ) ) { | ||
| $parent_entry_id = rgget( 'edit_entry' ); | ||
| } | ||
| return $parent_entry_id; | ||
| } ); |
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.
🛠️ Refactor suggestion
Sanitize and type-cast edit_entry; avoid double rgget to prevent accidental 0 parent IDs
Cast to int once and reuse. This prevents non-numeric values from coercing to 0 and unintentionally overriding the parent ID.
Apply this diff:
-add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
- if ( rgget( 'edit_entry' ) ) {
- $parent_entry_id = rgget( 'edit_entry' );
- }
- return $parent_entry_id;
-} );
+add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
+ $edit_entry = absint( rgget( 'edit_entry' ) );
+ if ( $edit_entry ) {
+ $parent_entry_id = $edit_entry;
+ }
+ return $parent_entry_id;
+} );📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | |
| if ( rgget( 'edit_entry' ) ) { | |
| $parent_entry_id = rgget( 'edit_entry' ); | |
| } | |
| return $parent_entry_id; | |
| } ); | |
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | |
| $edit_entry = absint( rgget( 'edit_entry' ) ); | |
| if ( $edit_entry ) { | |
| $parent_entry_id = $edit_entry; | |
| } | |
| return $parent_entry_id; | |
| } ); |
🤖 Prompt for AI Agents
In gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php around lines 12 to
17, the current code calls rgget('edit_entry') twice and does not
sanitize/type-cast the value; change it to read rgget('edit_entry') once into a
variable, check if it is non-empty, cast it to (int) and then assign to
$parent_entry_id only when the cast result is > 0 to avoid accidentally
overriding a valid parent ID with 0 from non-numeric input.
💡 Verification agent
🧩 Analysis chain
Verify “edit_entry” is the correct GP Entry Blocks param across this repo
Double-check consistency with existing snippets (e.g., gpeb-remove-fields-on-edit.php) and usages to avoid mismatched query params.
Run this script to confirm:
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Searching for existing snippets referencing edit_entry…"
rg -n "rgget\(\s*'edit_entry'\s*\)" -A 2 || true
echo
echo "Locating gpeb-remove-fields-on-edit.php (if present) and printing its contents…"
fd -i -a gpeb-remove-fields-on-edit.php | while read -r f; do
echo "---- $f ----"
sed -n '1,200p' "$f"
done
echo
echo "Search for query param name patterns possibly used by Entry Blocks:"
rg -n "(edit_entry|edit\-entry|entry_id|entry-id|eid)" -A 2 || trueLength of output: 96754
Standardize on the GP Entry Blocks query parameter
The only occurrence of rgget('edit_entry') is in gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php, but all other Entry Blocks snippets expect the standard gpeb_entry_id parameter. Update this hook to read the correct param (or support both for backward compatibility).
Locations to update:
- gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php (lines 12–17)
Proposed change:
add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) {
- if ( rgget( 'edit_entry' ) ) {
- $parent_entry_id = rgget( 'edit_entry' );
- }
+ // Use the standard GP Entry Blocks parameter
+ $entry_id = rgget( 'gpeb_entry_id' ) ?: rgget( 'edit_entry' );
+ if ( $entry_id ) {
+ $parent_entry_id = $entry_id;
+ }
return $parent_entry_id;
} );📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | |
| if ( rgget( 'edit_entry' ) ) { | |
| $parent_entry_id = rgget( 'edit_entry' ); | |
| } | |
| return $parent_entry_id; | |
| } ); | |
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | |
| // Use the standard GP Entry Blocks parameter | |
| $entry_id = rgget( 'gpeb_entry_id' ) ?: rgget( 'edit_entry' ); | |
| if ( $entry_id ) { | |
| $parent_entry_id = $entry_id; | |
| } | |
| return $parent_entry_id; | |
| } ); |
🤖 Prompt for AI Agents
In gp-nested-forms/gpnf-gpeb-auto-attach-child-entries.php around lines 12 to
17, the hook currently reads rgget('edit_entry') but other Entry Blocks use the
standard gpeb_entry_id param; change the logic to read gpeb_entry_id first and
fall back to edit_entry for backward compatibility (i.e., if
rgget('gpeb_entry_id') is present use that, otherwise use rgget('edit_entry')),
then return the resolved parent_entry_id.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3026768611/87427
Summary
Auto-attach the child entry to the parent entry when editing via GP Entry Blocks, similar to https://github.com/gravitywiz/snippet-library/blob/master/gp-nested-forms/gpnf-gv-auto-attach-child-entries.php and https://github.com/gravitywiz/snippet-library/blob/master/gp-entry-blocks/gpeb-remove-fields-on-edit.php