Description
Allows users to filter the raw config data that eventually gets parsed into localized data. This is useful for adding in 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
Example
The following would result in the $config data for foobar to include the foobar value from get_option(‘foobar’), with a default mocked value of “Foo Bar”. This would then be parsed and combined into the localized data for foobar, using the mocked value or live value depending on 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 );
Context
This functionality allows the registration and manipulation of data to be localized, and this localized data can further be used to provide “mock data” for usage in things like Jest tests and Storybook components. To define mock data for a value, you need to set up your array with a value
parameter (for the “live” value), and a default
parameter (for the “mock” 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 ), )
This allows us to use a single config to provide data for both live contexts and mocked/testing contexts in a unified manner.
See Also
Related filter: gform_localized_script_data_$name.
Since
This hook was added in Gravity Forms v2.6.
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?
Source Code
This action hook is located in includes/config/class-gf-config.php.