|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Nested Forms // Metabox on Entry Details to detach child entry from parent. |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 5 | + * |
| 6 | + * Instruction Video: https://www.loom.com/share/84a6fc86c75449baa544cd8080c3ed15 |
| 7 | + */ |
| 8 | +add_filter( 'gform_entry_detail_meta_boxes', function ( $meta_boxes, $entry, $form ) { |
| 9 | + if ( class_exists( 'GP_Nested_Forms' ) ) { |
| 10 | + // Only to be displayed for Nested Entries. |
| 11 | + $parent_entry_id = rgar( $entry, 'gpnf_entry_parent' ); |
| 12 | + if ( ! $parent_entry_id ) { |
| 13 | + return $meta_boxes; |
| 14 | + } |
| 15 | + |
| 16 | + // Either the metabox shows when Nested entry is still attached, or the metabox is skipped when parent link is detached. |
| 17 | + $action = 'gpnf_detach_parent_entry'; |
| 18 | + if ( rgpost( 'action' ) == $action ) { |
| 19 | + $entry_id = $entry['id']; |
| 20 | + gform_delete_meta( $entry_id, 'gpnf_entry_parent' ); |
| 21 | + gform_delete_meta( $entry_id, 'gpnf_entry_parent_form' ); |
| 22 | + } else { |
| 23 | + $meta_boxes['gpnf_metabox'] = array( |
| 24 | + 'title' => 'Nested Forms', |
| 25 | + 'callback' => 'detach_parent_entry_from_child', |
| 26 | + 'context' => 'side', |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return $meta_boxes; |
| 32 | +}, 10, 3 ); |
| 33 | + |
| 34 | +// Callback method |
| 35 | +function detach_parent_entry_from_child( $args ) { |
| 36 | + $action = 'gpnf_detach_parent_entry'; |
| 37 | + $html = sprintf( '<input type="submit" value="%s" class="button" onclick="jQuery(\'#action\').val(\'%s\');" />', 'Detach from Parent', $action ); |
| 38 | + |
| 39 | + echo $html; |
| 40 | +} |
0 commit comments