Description
Allows the HubSpot form object to be filtered before saving the feed.
Usage
The following would apply to all forms:
add_filter( 'gform_hubspot_form_object_pre_save_feed', 'your_function_name', 10, 4 );
To target a specific form, append the form id to the hook name. (format: gform_hubspot_form_object_pre_save_feed_FORMID)
add_filter( 'gform_hubspot_form_object_pre_save_feed_FORMID, 'your_function_name', 10, 4 );
Parameters
| Parameter | Type | Description |
|---|---|---|
| $hs_form | array | The new HubSpot form to be created or the properties to be updated for an existing HubSpot form. Refer to HubSpot’s documentation for more information on the data used in the array. |
| $feed_meta | array | The meta from the current Feed Object. |
| $form | The current Gravity Forms form object. | |
| $existing_form | null|array | Null or the existing HubSpot form. |
Examples
Add submit button text using displayOptions.
add_filter( 'gform_hubspot_form_object_pre_save_feed', 'change_hubspot_feed', 10, 4 );
function change_hubspot_feed( $hs_form, $feed_meta, $form, $existing_form ) {
if ( ! isset( $hs_form['displayOptions'] ) ) {
$hs_form['displayOptions'] = rgar( $existing_form, 'displayOptions', array() );
}
$hs_form['displayOptions']['submitButtonText'] = 'Submit the Form';
return $hs_form;
}
Always create a contact for new email addresses and pre-populate known values.
add_filter( 'gform_hubspot_form_object_pre_save_feed', function ( $hs_form, $feed_meta, $form, $existing_form ) {
if ( ! isset( $hs_form['configuration'] ) ) {
$hs_form['configuration'] = rgar( $existing_form, 'configuration', array() );
}
// Always create contact for new email address.
$hs_form['configuration']['createNewContactForNewEmail'] = true;
// Pre-populate contact fields with known values.
$hs_form['configuration']['prePopulateKnownValues'] = true;
return $hs_form;
}, 10, 4 );
Remove hubspot_owner_id field
Some HubSpot accounts don’t support using the Contact Owner field with forms, which can result in an error when saving a feed. The following shows how you can remove the field from the HubSpot form object before it is sent to HubSpot.
add_filter( 'gform_hubspot_form_object_pre_save_feed', function ( $hs_form ) {
foreach ( $hs_form['fieldGroups'] as &$group ) {
if ( empty( $group['fields'] ) ) {
continue;
}
foreach ( $group['fields'] as $key => $field ) {
if ( rgar( $field, 'name' ) !== 'hubspot_owner_id' ) {
continue;
}
unset( $group['fields'][ $key ] );
$group['fields'] = array_values( $group['fields'] );
break;
}
}
return $hs_form;
} );
Set language code for new HubSpot form
The following example shows how you can set the language code for a new HubSpot form just before it is created. See the HubSpot API documentation for the supported codes.
add_filter( 'gform_hubspot_form_object_pre_save_feed', function ( $hs_form, $feed_meta, $form, $existing_form ) {
if ( empty( $existing_form ) ) {
$hs_form['configuration']['language'] = 'zh-tw';
}
return $hs_form;
}, 10, 4 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
This filter was added in HubSpot version 1.0.
Source Code
This filter is located in GF_HubSpot::generate_hubspot_form_object() in class-gf-hubspot.php.