Skip to content

Commit 0ed5edb

Browse files
authored
gpnf-add-child-entry-on-render.php: Created new snippet.
1 parent fadae2d commit 0ed5edb

File tree

1 file changed

+69
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)