Skip to content

Commit 1a4370a

Browse files
authored
gpnf-add-child-entry-on-render.php: Migrated.
1 parent 56e6249 commit 1a4370a

File tree

1 file changed

+3
-74
lines changed

1 file changed

+3
-74
lines changed
Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,6 @@
11
<?php
22
/**
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.
3+
* We're no longer using the experimental folder for experimental snippets. 🚧
4+
* You can now find the snippet here:
5+
* https://github.com/gravitywiz/snippet-library/blob/master/gp-nested-forms/gpnf-add-child-entry-on-render.php
106
*/
11-
// Update "123" to your form ID and "4" to your Nested Form field ID.
12-
add_filter( 'gpnf_submitted_entry_ids_123_4', 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' => time(),
21-
// Add as many "field ID" => "value" pairs as required.
22-
), $parent_form['id'] );
23-
24-
$session = new GPNF_Session( $parent_form['id'] );
25-
26-
// Attach new child entry to the session.
27-
$session->add_child_entry( $child_entry_id );
28-
29-
// Get all entry IDs from the session and return them.
30-
$session_entry_ids = $session->get( 'nested_entries' );
31-
if ( ! empty( $session_entry_ids[ $nested_form_field->id ] ) ) {
32-
$entry_ids = $session_entry_ids[ $nested_form_field->id ];
33-
}
34-
35-
return $entry_ids;
36-
}, 10, 3 );
37-
38-
if ( ! function_exists( 'gpnf_session_hash' ) ) {
39-
function gpnf_session_hash( $form_id ) {
40-
$session = new GPNF_Session( $form_id );
41-
return $session->get_runtime_hashcode();
42-
}
43-
}
44-
45-
if ( ! function_exists( 'gpnf_add_child_entry' ) ) {
46-
/**
47-
* @param int $parent_entry_id The ID of the entry to which this child entry should be attached.
48-
* @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.
49-
* @param array $field_values An array of field values that will be used to created the child entry (e.g. array( 1 => 'value' )).
50-
* @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.
51-
*/
52-
function gpnf_add_child_entry( $parent_entry_id, $nested_form_field_id, $field_values = array(), $parent_form_id = false ) {
53-
54-
if ( ! $parent_form_id ) {
55-
$parent_entry = GFAPI::get_entry( $parent_entry_id );
56-
$parent_form_id = $parent_entry['form_id'];
57-
}
58-
59-
$nested_form_field = GFAPI::get_field( $parent_form_id, $nested_form_field_id );
60-
61-
$new_child_entry = array_replace( array(
62-
// The ID of the parent form.
63-
'form_id' => $nested_form_field->gpnfForm,
64-
'created_by' => null,
65-
// The ID of the parent entry.
66-
GPNF_Entry::ENTRY_PARENT_KEY => $parent_entry_id,
67-
// The ID of the parent form.
68-
GPNF_Entry::ENTRY_PARENT_FORM_KEY => $parent_form_id,
69-
// The ID of the Nested Form field on the parent form.
70-
GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY => $nested_form_field_id,
71-
), $field_values );
72-
73-
$new_child_entry_id = GFAPI::add_entry( $new_child_entry );
74-
75-
return $new_child_entry_id;
76-
}
77-
}

0 commit comments

Comments
 (0)