Using Gravity Forms ‘gform_config_data_$name’ PHP action

The gform_config_data_$name Gravity Forms PHP filter allows users to filter the raw config data that eventually gets parsed into localized data. This is useful for adding mock data for a given value.

Usage

add_filter('gform_config_data_foobar', 'your_function_name', 10, 2);

Parameters

  • $config_data (array) – The raw config data to be applied to the script
  • $script (string) – The script being localized

More information

See Gravity Forms Docs: gform_config_data_$name

This functionality enables the registration and manipulation of data to be localized, which can further be used to provide mock data for usage in things like Jest tests and Storybook components.

Examples

Add a custom filter

This example adds a custom filter that sets the $config_data for ‘foobar’ to include the ‘foobar’ value from get_option('foobar'), with a default mocked value of “Foo Bar”. This data is then parsed and combined into the localized data for ‘foobar’, using the mocked value or live value depending on the context.

add_filter('gforms_config_data_foobar', function($config_data, $script) {
    $config_data['foobar']['value'] = get_option('foobar');
    $config_data['foobar']['default'] = 'Foo Bar';
    return $config_data;
}, 10, 2);

Define mock data for a value

$data = array(
    'foobar' => 'basic value', // foobar will have a single value in both mock and live contexts
    'foobash' => array(
        'value' => get_option('foobash'), // value to be used in live contexts
        'default' => 'Foo Bash Default', // value to be used in mocked contexts
    ),
);

Filter config data for custom value

add_filter('gform_config_data_custom_value', 'filter_custom_value', 10, 2);

function filter_custom_value($config_data, $script) {
    $config_data['custom_value']['value'] = get_option('custom_value');
    $config_data['custom_value']['default'] = 'Custom Default Value';
    return $config_data;
}

Filter config data for multiple custom values

add_filter('gform_config_data_multiple_values', 'filter_multiple_values', 10, 2);

function filter_multiple_values($config_data, $script) {
    $config_data['custom_value1']['value'] = get_option('custom_value1');
    $config_data['custom_value1']['default'] = 'Custom Default Value 1';
    $config_data['custom_value2']['value'] = get_option('custom_value2');
    $config_data['custom_value2']['default'] = 'Custom Default Value 2';
    return $config_data;
}

Filter config data for a custom script

add_filter('gform_config_data_custom_script', 'filter_custom_script', 10, 2);

function filter_custom_script($config_data, $script) {
    if ($script === 'custom_script') {
        $config_data['custom_value']['value'] = get_option('custom_value');
        $config_data['custom_value']['default'] = 'Custom Default Value';
    }
    return $config_data;
}