gform_localized_script_data_$name

Description

Allows users to filter the data being localized for a given localized object $name.

Usage

add_filter( 'gform_localized_script_data_foobar', 'your_function_name', 10, 3 );

Parameters

  • $localized_data array
    The current localize data
  • $script string
    The script being localized
  • $configs array
    An array of $configs being applied to this script

Example

Include the value grabbed from get_option('foobar'); in the localized data for the foobar object.

add_filter( 'gforms_localized_script_data_foobar', function( $localized_data, $script, $configs ) {

    $localized_data['foobar_option'] = get_option( 'foobar' );

    return $localized_data;

}, 10, 3 );

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_config_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-collection.php.