Skip to content

Commit d3de598

Browse files
authored
gpnf-add-child-entry-on-render.php: Migrated from experimental folder.
1 parent 56e6249 commit d3de598

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Nested Forms // Add Child Entry on Render
4+
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
5+
*
6+
* Experimental Snippet 🧪
7+
*
8+
* Programattically create and attach a child entry to a Nested Form field when the parent form is rendered.
9+
*
10+
* Please note: A new child entry will be added on every render. You will need to identify your own condition
11+
* for when a child entry should be generated and attached.
12+
*/
13+
// Update "123" to your form ID and "4" to your Nested Form field ID.
14+
add_filter( 'gpnf_submitted_entry_ids_123_4', function( $entry_ids, $parent_form, $nested_form_field ) {
15+
16+
$hash = gpnf_session_hash( $parent_form['id'] );
17+
18+
$child_entry_id = gpnf_add_child_entry( $hash, $nested_form_field->id, array(
19+
// Update "1" to any field ID from your child form and "Second Choice" the value that should be saved to this field.
20+
'1' => 'Second Choice',
21+
// Update "2" to another field ID in your child field and "Second" to the value that should be saved to it.
22+
'2' => time(),
23+
// Add as many "field ID" => "value" pairs as required.
24+
), $parent_form['id'] );
25+
26+
$session = new GPNF_Session( $parent_form['id'] );
27+
28+
// Attach new child entry to the session.
29+
$session->add_child_entry( $child_entry_id );
30+
31+
// Get all entry IDs from the session and return them.
32+
$session_entry_ids = $session->get( 'nested_entries' );
33+
if ( ! empty( $session_entry_ids[ $nested_form_field->id ] ) ) {
34+
$entry_ids = $session_entry_ids[ $nested_form_field->id ];
35+
}
36+
37+
return $entry_ids;
38+
}, 10, 3 );
39+
40+
if ( ! function_exists( 'gpnf_session_hash' ) ) {
41+
function gpnf_session_hash( $form_id ) {
42+
$session = new GPNF_Session( $form_id );
43+
return $session->get_runtime_hashcode();
44+
}
45+
}
46+
47+
if ( ! function_exists( 'gpnf_add_child_entry' ) ) {
48+
/**
49+
* @param int $parent_entry_id The ID of the entry to which this child entry should be attached.
50+
* @param int $nested_form_field_id The ID of the Nested Form field on the parent form to which this child entry should be attached.
51+
* @param array $field_values An array of field values that will be used to created the child entry (e.g. array( 1 => 'value' )).
52+
* @param int $parent_form_id The ID of the parent entry's form. If not provided, will be looked up based on the provided parent entry ID.
53+
*/
54+
function gpnf_add_child_entry( $parent_entry_id, $nested_form_field_id, $field_values = array(), $parent_form_id = false ) {
55+
56+
if ( ! $parent_form_id ) {
57+
$parent_entry = GFAPI::get_entry( $parent_entry_id );
58+
$parent_form_id = $parent_entry['form_id'];
59+
}
60+
61+
$nested_form_field = GFAPI::get_field( $parent_form_id, $nested_form_field_id );
62+
63+
$new_child_entry = array_replace( array(
64+
// The ID of the parent form.
65+
'form_id' => $nested_form_field->gpnfForm,
66+
'created_by' => null,
67+
// The ID of the parent entry.
68+
GPNF_Entry::ENTRY_PARENT_KEY => $parent_entry_id,
69+
// The ID of the parent form.
70+
GPNF_Entry::ENTRY_PARENT_FORM_KEY => $parent_form_id,
71+
// The ID of the Nested Form field on the parent form.
72+
GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY => $nested_form_field_id,
73+
), $field_values );
74+
75+
$new_child_entry_id = GFAPI::add_entry( $new_child_entry );
76+
77+
return $new_child_entry_id;
78+
}
79+
}

0 commit comments

Comments
 (0)