Filter Resource Fields by Resource Type

Limits individual Resource fields to configured Resource Types when the connected Service field uses Manual Selection.

Instructions

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

  2. Set formId to the ID of your form.

  3. Map each Resource field ID to its desired Resource Type IDs in resourceTypesByFieldId.

Code

Filename: gpb-filter-resource-fields-by-type.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
/**
 * Gravity Perks // Bookings // Filter Resource Fields by Resource Type
 * https://gravitywiz.com/documentation/gravity-forms-bookings/
 *
 * Limits individual Resource fields to configured Resource Types when the
 * connected Service field uses Manual Selection.
 *
 * Instructions
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Set `formId` to the ID of your form.
 *
 * 3. Map each Resource field ID to its desired Resource Type IDs in
 *    `resourceTypesByFieldId`.
 */
window.gform.addFilter(
	'gpb_resource_field_resources',
	function (resources, context) {
		var formId = 123;
		var resourceTypesByFieldId = {
			4: [10, 11], // Resource field ID 4 => Resource Type IDs 10 and 11.
			5: [12], // Resource field ID 5 => Resource Type ID 12.
		};

		if (Number(context.formId) !== formId) {
			return resources;
		}

		var resourceTypeIds = resourceTypesByFieldId[context.fieldId];

		if (!resourceTypeIds) {
			return resources;
		}

		return resources.filter(function (resource) {
			return resourceTypeIds.includes(Number(resource.resourceType));
		});
	}
);

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.