|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Forms // Nested Forms // Capture Nested Form Field Label in Child Form |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 5 | + * |
| 6 | + * Instruction Video: https://www.loom.com/share/1630b49c960441e8a5d13b0d5c2fa3e8 |
| 7 | + * |
| 8 | + * This snippet allows you to capture the label of the Nested Form field from which the child |
| 9 | + * form has been opened. |
| 10 | + * |
| 11 | + * This is useful when you have two or more Nested Form fields on the same parent form, using |
| 12 | + * the same child form and need to identify to which Nested Form field the child entry belongs |
| 13 | + * (e.g. Team Captains vs Players). |
| 14 | + * |
| 15 | + * Instructions: |
| 16 | + * |
| 17 | + * 1. Install the snippet. |
| 18 | + * https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets |
| 19 | + * |
| 20 | + * 2. Enable dynamic population on your desired field on the child form. |
| 21 | + * |
| 22 | + * 3. Set the dynamic population parameter to "gpnf_nested_form_field_label". |
| 23 | + */ |
| 24 | +add_filter( 'gform_field_value_gpnf_nested_form_field_label', function () { |
| 25 | + |
| 26 | + // Get the parent form ID |
| 27 | + $form_id = gp_nested_forms()->get_parent_form_id(); |
| 28 | + if ( ! $form_id ) { |
| 29 | + return ''; |
| 30 | + } |
| 31 | + |
| 32 | + // Retrieve the form object |
| 33 | + $form = GFAPI::get_form( $form_id ); |
| 34 | + if ( ! $form ) { |
| 35 | + return ''; |
| 36 | + } |
| 37 | + |
| 38 | + // Get the posted Nested Form field ID |
| 39 | + $nested_form_field_id = rgar( $_REQUEST, 'gpnf_nested_form_field_id' ); |
| 40 | + if ( ! $nested_form_field_id ) { |
| 41 | + return ''; |
| 42 | + } |
| 43 | + |
| 44 | + // Get the Nested Form field object |
| 45 | + $nested_form_field = GFFormsModel::get_field( $form, $nested_form_field_id ); |
| 46 | + if ( ! $nested_form_field || ! is_object( $nested_form_field ) ) { |
| 47 | + return ''; |
| 48 | + } |
| 49 | + |
| 50 | + // Return the field label |
| 51 | + return $nested_form_field->get_field_label( false, '' ); |
| 52 | +} ); |
0 commit comments