Ga naar inhoud

Prefill hooks

Deze inhoud is nog niet vertaald.

The Prefill add-on fills a form from an external source before it is rendered. It saves a Gravity Forms draft and resumes it, so the visitor sees the form with the values already in place. Salesforce is the built-in source. With these hooks you can add sources, skip prefill and adjust the form arguments.

Filter · includes/gravityforms/addons/class-prefilladdon.php

Collects the available prefill sources. The Prefill add-on only adds its form settings page when at least one source is registered. The Salesforce source (salesforce) is added through this filter when it is enabled.

Parameter Type Description
$sources array<string, array> Sources keyed by source ID. Default []. Each source has label, handle_prefill_callback and settings.

handle_prefill_callback is called as ($form, $settings, $prefill_addon) and returns an array of values keyed by field or input ID (for example '3' => 'Jane' or '4.3' => 'Amsterdam'). Empty values are ignored. settings is an array of Gravity Forms settings sections, or a callable ($form, $prefill_addon) returning them.

Returns: array<string, array> — the sources.

add_filter('cs_forms_prefill_sources', function ($sources) {
$sources['my_crm'] = [
'label' => __('My CRM', 'my-plugin'),
'handle_prefill_callback' => function ($form, $settings, $prefill_addon) {
$token = sanitize_text_field(wp_unslash($_GET['contact'] ?? ''));
$contact = $token ? my_crm_find_contact($token) : null;
return $contact ? ['1' => $contact['first_name'], '2' => $contact['email']] : [];
},
'settings' => [],
];
return $sources;
});

Filter · includes/gravityforms/addons/class-prefilladdon.php

Lets you switch prefill off for a form on this render. Return true and the form is rendered without prefill. It runs for every form render that is not a block editor view, a preview or a form submission.

Parameter Type Description
$prevent bool The result of cs_forms_prevent_prefill_{form_id}. Default false.
$form_id int The form ID.
$form_args array The form arguments, as passed to gform_form_args.

Returns: bool — true to prevent prefill.

add_filter('cs_forms_prevent_prefill', function ($prevent, $form_id, $form_args) {
return $prevent || isset($_GET['no_prefill']);
}, 10, 3);

Filter · includes/gravityforms/addons/class-prefilladdon.php

The same switch for one form. {form_id} is the numeric form ID, for example cs_forms_prevent_prefill_12. It runs before cs_forms_prevent_prefill, which receives its result. The plugin itself adds __return_true to this filter after a payment return, so a confirmation or a restored failed submission is not prefilled again.

Parameter Type Description
$prevent bool Default false.

Returns: bool — true to prevent prefill.

add_filter('cs_forms_prevent_prefill_12', '__return_true');

Filter · includes/gravityforms/addons/class-prefilladdon.php

Filters the form arguments after the Prefill add-on is done with them for this render. It runs whether or not the form was prefilled. When it was, the gf_token request value points at the prefill draft. If the form is hidden because “Only show form when prefilled” is on and the source returned nothing, form_id is false.

Parameter Type Description
$form_args array The gform_form_args value: form_id, field_values, display_title and so on.

Returns: array — the form arguments.

add_filter('cs_forms_prefilled_form_args', function ($form_args) {
if (!empty($_GET['gf_token'])) {
$form_args['display_title'] = false;
}
return $form_args;
});

cs_forms_salesforce_prefill_cache_expiration

Section titled “cs_forms_salesforce_prefill_cache_expiration”

Filter · includes/class-salesforce.php

Filters how long a Salesforce prefill lookup is cached, for both a match and a miss. Every new value in a prefill URL adds a cache entry, and a match holds the record’s data, so keep this short. The default is 5 minutes.

Parameter Type Description
$expiration int Seconds. Default 300 (5 minutes). A value of 0 or less turns caching off.

Returns: int — the number of seconds.

add_filter('cs_forms_salesforce_prefill_cache_expiration', function ($expiration) {
return 60;
});