Duplicate Child Entry Multiple Times

Prompts for a quantity and runs the duplication that many times.

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

  2. Configure the snippet’s fieldId, maxCopies and prompt

Code

Filename: gpnf-duplicate-child-entry-multiple-times.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * Gravity Perks // Nested Forms // Duplicate Child Entry Multiple Times
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Prompts for a quantity and runs the duplication that many times.
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Configure the snippet's fieldId, maxCopies and prompt
 */
(function( $ ) {

	// Configuration
	var CONFIG = {
		targets: [
			{
				fieldId: 1,      // Nested Form Field ID.
				// maxCopies: 5, // Optional cap per duplicate action.
				// prompt: '',   // Override the default prompt.
			},
		],
	};

	function init( gpnf ) {
		var config;

		// Match the active GPNestedForms instance to the configured targets.
		for ( var i = 0; i < CONFIG.targets.length; i++ ) {
			if ( CONFIG.targets[ i ].fieldId === gpnf.fieldId && matchesForm( gpnf ) ) {
				config = CONFIG.targets[ i ];
				break;
			}
		}

		if ( ! config || gpnf._multiDuplicateReady ) {
			return;
		}

		gpnf._multiDuplicateReady = true;

		// Wrap GPNF's duplicate endpoint in a promise so we can queue requests sequentially.
		var duplicateOnce = (function() {
			var nonce = window.GPNFData && window.GPNFData.nonces ? window.GPNFData.nonces.duplicateEntry : '';

			return function( entryId ) {
				return $.post( gpnf.ajaxUrl, {
					action: 'gpnf_duplicate_entry',
					nonce: nonce,
					gpnf_entry_id: entryId,
					gpnf_parent_form_id: gpnf.formId,
					gpnf_nested_form_field_id: gpnf.fieldId,
					gpnf_context: gpnf.ajaxContext
				} ).then( function( response ) {
					if ( ! response || ! response.success ) {
						return $.Deferred().reject( response && response.data ? response.data : 'Unable to duplicate entry.' );
					}

					if ( window.GPNestedForms && typeof window.GPNestedForms.loadEntry === 'function' ) {
						window.GPNestedForms.loadEntry( response.data );
					}

					if ( window.gform && typeof window.gform.doAction === 'function' ) {
						window.gform.doAction( 'gpnf_post_duplicate_entry', response.data.entry, response );
					}

					return response;
				} );
			};
		})();

		gpnf.duplicateEntry = function( entryId, $trigger ) {

			var message = config.prompt || 'How many times should this entry be duplicated?';
			var input = window.prompt( message, '1' );

			if ( input === null ) {
				return;
			}

			var copies = parseInt( input, 10 );

			if ( isNaN( copies ) || copies < 1 ) {
				copies = 1;
			}

			if ( typeof config.maxCopies === 'number' ) {
				copies = Math.min( copies, config.maxCopies );
			}

			var max = gpnf.entryLimitMax;

			if ( window.gform && gform.applyFilters ) {
				max = gform.applyFilters( 'gpnf_entry_limit_max', max, gpnf.formId, gpnf.fieldId, gpnf );
			}

			if ( max !== '' && max != null ) {
				max = parseInt( max, 10 );

				if ( ! isNaN( max ) ) {
					var current = gpnf.viewModel && gpnf.viewModel.entries ? gpnf.viewModel.entries().length : 0;
					copies = Math.min( copies, Math.max( max - current, 0 ) );
				}
			}

			if ( copies < 1 ) {
				return;
			}

			var disableTarget = $trigger && typeof $trigger.prop === 'function' ? $trigger : null;

			if ( disableTarget ) {
				disableTarget.prop( 'disabled', true );
			}

			var chain = $.Deferred().resolve();
			for ( var i = 0; i < copies; i++ ) {
				chain = chain.then( function() {
					return duplicateOnce( entryId );
				} );
			}

			chain.fail( function( message ) {
				if ( message ) {
					window.alert( message );
				}
			} ).always( function() {
				if ( disableTarget ) {
					disableTarget.prop( 'disabled', false );
				}
			} );
		};
	}

	function matchesForm( gpnf ) {
		if ( typeof gpnf.formId !== 'number' ) {
			return false;
		}

		if ( typeof GFFORMID !== 'undefined' ) {
			var currentFormId = parseInt( GFFORMID, 10 );

			if ( ! isNaN( currentFormId ) ) {
				return gpnf.formId === currentFormId;
			}
		}

		return true;
	}

	if ( window.gform && gform.addAction ) {
		gform.addAction( 'gpnf_session_initialized', init );
	}

	for ( var key in window ) {
		if ( key.indexOf( 'GPNestedForms_' ) === 0 ) {
			init( window[ key ] );
		}
	}

})( window.jQuery );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.