Counting the number of entries in a repeater field group
-
In a form with a repeater field group, is it possible to count the number of entries, and show that number somewhere else on the form ?
I have a registration form allowing multiple people to be registered, and I would like to have the number of registered persons to be displayed on the bottom of the form.
-
Hi @jpgoem
I hope you are doing well today.
I pinged our SLS Team to review your query and see what will be possible in this case. We will post an update here as soon as more information is available.
Kind Regards,
KrisHi again @jpgoem
Please try this code as a mu-plugin:
add_action( 'wp_footer', 'wpmudev_group_repeater_count', 9999 ); function wpmudev_group_repeater_count() { global $post; if ( is_a( $post, 'WP_Post' ) && !has_shortcode($post->post_content, 'forminator_form') ) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); },100); $(document).on('after.load.forminator', function(e, form_id) { if ( e.target.id == 'forminator-module-2960' ) { // Please change the form ID. $('<p class="repeater-count">1</p>').insertBefore('.forminator-row-last'); $(document).on('forminator:recalculate', function(){ $('.repeater-count').remove(); var participant_count = $( '.forminator-grouped-fields' ).length; $('<p class="repeater-count">'+participant_count+'</p>').insertBefore('.forminator-row-last'); }); } }); }); </script> <?php }
Note to change 2960 to your form ID.
You can follow our docs here for more detailed steps on how to install a?mu-plugin.
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsKind Regards,
KrisHello Kris,
Thank you for your answer and the plugin. I installed the mu-plugin and changed the form ID.
I now see the number of entries at the bottom of the form (just above the submit button).
But I now realize that I didn’t formulate my question quite correctly. What I would like is that the counter can be included in a (calculation) field, so that it also appears in the submissions table. Optionally, I can use the “hidden” setting to display the field on the form or not.
Is this feasible?Hi @jpgoem
It appears we didn’t get a notification regarding this thread and hence the delay in following up. Sorry about that.
I’m checking with our developer if there is any workaround that could be suggested and will get back asap.
Kind Regards,
Nithin
We just got response form our developers with a bit different solution.
1. First, you’d need to add an additional “calculation” type field to the form and set its calculation formula to the value of 1 (just put number 1 in the formula field setting and save it); note down the field ID (e.g. calculation-5 or whatever ID it gets on your form)
2. then replace previously shared code with this one
add_action( 'wp_footer', 'wpmudev_group_repeater_count', 9999 ); function wpmudev_group_repeater_count() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); },100); $(document).on('after.load.forminator', function(e, form_id) { if ( e.target.id == 'forminator-module-2960' ) { // Please change the form ID. $(document).on('forminator:recalculate', function() { var participant_count = $( '#group-1 .forminator-grouped-fields' ).length; $('#calculation-1 input').val(participant_count).trigger('change'); }); } }); }); </script> <?php } add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_pcount_formula_change', 10, 3 ); function wpmudev_pcount_formula_change( $entry, $module_id, $field_data_array ) { $form_ids = array( 2960 ); // Please change the form ID. if ( ! in_array( intval( $module_id ), $form_ids, true ) ) { return; } $prepared_data = Forminator_CForm_Front_Action::$prepared_data; foreach ( $field_data_array as $key => $value ) { if ( 'calculation-1' === $value['name'] ) { // Please change the field ID. Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['formatting_result'] += count( $prepared_data['group-1-copies'] ); Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['result'] += count( $prepared_data['group-1-copies'] ); } } }
3. Finally, in that code you need to make some small adjustments:
a) replace both occurences of number 2960 with your form’s ID; form ID is the number you see in form’s shortcode
b) adjust calculation field ID in these two lines; basically you’d just need to change the number to match your newly added calculation field;
1st line:
$('#calculation-1 input').val(participant_count).trigger('change');
2nd line:
if ( 'calculation-1' === $value['name'] ) { // Please change the field ID.
c) and adjust number in “group=1-copies” in these three lines to match your field group ID (again, you’d just need to adjust the number)
var participant_count = $( '#group-1 .forminator-grouped-fields' ).length;
Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['formatting_result'] += count( $prepared_data['group-1-copies'] );
Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['result'] += count( $prepared_data['group-1-copies'] );
Then it should show the counted number in that new calculations field (and it will be included in submission).
Best regards,
AdamHi Adam,
Thank you for your answer and the modified mu plugin.
I tested it in detail with my form and it works almost perfectly….
- The field appears correctly on the form.
- The field appears correctly in the submissions table.
- In the notification emails, the field appears correctly in the {all_fields} and {all_non_empty_fields} lists.
- In the notification emails, the field appears incorrectly (value is always 1) if it is explicitly inserted in the email ({calculation-4} in my form).
Any hint?
And also another problem: if certain fields (such as select fields) in the form are changed, the counter field is reset.
Hi @jpgoem
Thank you for response!
In that case I need to take it back to our developers. I’ve already asked them if they could provide necessary adjustments to address those issues and we’ll let you know here soon.
Best regards,
AdamHi @jpgoem
I just got response from our developers with a new version of the code. Would you please try it?
add_action( 'wp_footer', 'wpmudev_group_repeater_count', 9999 ); function wpmudev_group_repeater_count() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); },100); $(document).on('after.load.forminator', function(e, form_id) { if ( e.target.id == 'forminator-module-2960' ) { // Please change the form ID. $(document).on('forminator:recalculate', function() { var participant_count = $( '#group-1 .forminator-grouped-fields' ).length; $('#calculation-1 input').val(participant_count).trigger('change'); }); $(document).on('forminator:field:condition:toggled', function() { setTimeout(function() { var participant_count = $( '#group-1 .forminator-grouped-fields' ).length; $('#calculation-1 input').val(participant_count).trigger('change'); },1000); }); } }); }); </script> <?php } add_filter( 'forminator_prepared_data', 'wpmudev_update_pcount_field_val', 10, 2 ); function wpmudev_update_pcount_field_val( $prepared_data, $module_object ){ $form_ids = array( 2960 ); // Please change the form ID if ( ! in_array( $module_object->id, $form_ids ) ) { return $prepared_data; } $prepared_data['calculation-1'] += count( $prepared_data['group-1-copies'] ); return $prepared_data; } add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_pcount_formula_change', 10, 3 ); function wpmudev_pcount_formula_change( $entry, $module_id, $field_data_array ) { $form_ids = array( 2960 ); // Please change the form ID. if ( ! in_array( intval( $module_id ), $form_ids, true ) ) { return; } $prepared_data = Forminator_CForm_Front_Action::$prepared_data; foreach ( $field_data_array as $key => $value ) { if ( 'calculation-1' === $value['name'] ) { // Please change the field ID. Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['formatting_result'] += count( $prepared_data['group-1-copies'] ); Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value']['result'] += count( $prepared_data['group-1-copies'] ); } } }
Note:
1. you’d need to replace previous one with this one
2. and make adjustments to it (field group IDs, form ID) the same way as described in my previous response.
Hopefully this one will work better for you.
Best regards,
AdamHi Adam,
Everything works fine now. Thank you for the wonderful support !
One last question: couldn’t this count become a standard feature of the plugin? I saw a similar question on https://wpmudev.com/forums/topic/forminator-pro-counting-repeater-field-groups-displaying-the-number/
- The topic ‘Counting the number of entries in a repeater field group’ is closed to new replies.