Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions gp-file-upload-pro/gpfup-count-files-groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Gravity Perks // File Upload Pro // Count Files in Groups
* https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
*
* Instruction Video: https://www.loom.com/share/2328e327125844bebdcabf7c9baaabca
*/
const formId = GFFORMID;

// Map count field IDs to arrays of file upload field IDs
const countMapping = {
5: [1, 3, 4], // Number Field ID 5 counts files from File Upload Field IDs 1, 3, 4
10: [7, 8, 9] // Number Field ID 10 counts files from File Upload Field IDs 7, 8, 9
// Add more mappings as needed: countFieldID: [uploadFieldID1, uploadFieldID2, ...]
};

// Find all GPFUP keys for the form
var gpfupInstances = Object.keys(window).filter(function (key) {
return key.startsWith('GPFUP_' + formId + '_');
});

// Build reverse lookup: uploadFieldID => associated countFieldIDs
const uploadToCountMap = {};
Object.entries(countMapping).forEach(([countFieldID, uploadFieldIDs]) => {
uploadFieldIDs.forEach((uploadFieldID) => {
if (!uploadToCountMap[uploadFieldID]) {
uploadToCountMap[uploadFieldID] = [];
}
uploadToCountMap[uploadFieldID].push(parseInt(countFieldID));
});
});

// Function to update all relevant count fields
function updateAllCountFields() {
Object.entries(countMapping).forEach(([countFieldID, uploadFieldIDs]) => {
const total = uploadFieldIDs.reduce((sum, uploadFieldID) => {
const key = 'GPFUP_' + formId + '_' + uploadFieldID;
const store = window[key]?.$store;
return sum + (store ? (store.state.files.length || 0) : 0);
}, 0);

const selector = '#input_' + formId + '_' + countFieldID;
const $field = jQuery(selector);
if ($field.length) {
$field.val(total).change();
}
});
}

if (gpfupInstances.length) {
// Subscribe to relevant GPFUP fields
gpfupInstances.forEach((key) => {
const parts = key.split('_');
const fieldID = parseInt(parts[2]); // GPFUP_formId_fieldId
const store = window[key].$store;

if (uploadToCountMap[fieldID]) {
store.subscribe((mutation, state) => {
if (mutation.type === 'SET_FILES') {
updateAllCountFields();
Copy link
Contributor

@veryspry veryspry Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since uploadToCountMap[fieldID] has an array of all count fields that need updating, what do you think about passing that as a param to updateAllCountFields() to avoid recalculating anything unnecessary count fields?

It would also need to account for the initial "update" for all count fields then too if making this suggested addition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}
});
}
});
}

// Initial count on load
updateAllCountFields();
Loading