gform_field_value_$parameter_name

gform_field_value_$parameter_name

DescriptionUsageParametersExamplesPopulate a fixed datePopulate from a cookieList FieldOld Array FormatNew Array FormatPopulate multiple fields using one functionPopulate the timePopulate current datePopulate previous valuePlacementSource Code

Description
This filter is executed before displaying each field and can be used to dynamically populate fields with a default value.
This filter can also be used to pre-select or pre-check drop down, radio button and checkbox items.
Note: This filter requires that the 「Allow field to be populated dynamically」 option is checked in the field editor』s advanced tab.
Usage
add_filter( 'gform_field_value_email', 'your_function_name' );

You can also use the filter without including a parameter name e.g.
add_filter( 'gform_field_value', 'your_function_name' );

Parameters

$value string
The string containing the current field value to be filtered.

$field Field Object
The current field being processed.

$name string
The parameter name of the field or input being processed.

Examples
Populate a fixed date
The following example populates the date field with a hard-coded value. It assumes that the date field is configured to 「Allow field to be populated dynamically」 and that the parameter name is set to 「date」
add_filter( 'gform_field_value_date', 'populate_date' );
function populate_date( $value ) {
return '10/10/2010';
}

Populate from a cookie
The following example populates a hidden field with content from a cookie. It assumes that the hidden field is configured to 「Allow field to be populated dynamically」 and that the parameter name is set to 「utm_campaign」 and the cookie has the same name.
add_filter( 'gform_field_value_utm_campaign', 'populate_utm_campaign' );
function populate_utm_campaign( $value ) {
return $_COOKIE['utm_campaign'];
}

List Field
The following examples populate a 3 column list field with some values. It assumes that the list field is configured to 「Allow field to be populated dynamically」 and that the parameter name is set to 「list」. The second example accepts a new array format that is available as of Gravity Forms 1.9.10.8. This format is the same format in which the data is saved in the database and much more user friendly. Example one is left for backwards compatibility since this format is still accepted.
Old Array Format
add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
return array( 'row 1 - col1', 'row 1 - col2', 'row 1 - col3',
'row 2 - col1', 'row 2 - col2', 'row 2 - col3',
'row 3 - col1', 'row 3 - col2', 'row 3 - col3' );

}

New Array Format
add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
$list_array = array(
array(
'Column 1' => 'row1col1',
'Column 2' => 'row1col2',
'Column 3' => 'row1col3',
),
array(
'Column 1' => 'row2col1',
'Column 2' => 'row2col2',
'Column 3' => 'row2col3'
),
);
return $list_array;
}

Populate multiple fields using one function
add_filter( 'gform_field_value', 'populate_fields', 10, 3 );
function populate_fields( $value, $field, $name ) {

$values = array(
'field_one' => 'value one',
'field_two' => 'value two',
'field_three' => 'value three',
);

return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}

Populate the time
The following example populates the time field with the current time. It assumes that the date field is configured to 「Allow field to be populated dynamically」 and that the parameter name is set to 「time」
add_filter( 'gform_field_value_time', 'populate_time' );
function populate_time( $value ) {
$local_timestamp = GFCommon::get_local_timestamp( time() );

return date_i18n( 'h:i A', $local_timestamp, true );
}

Populate current date
The following example populates a date field with the current date. It assumes that the date field is configured to 「Allow field to be populated dynamically」 and that the parameter name is set to 「current_date」
add_filter( 'gform_field_value_current_date', 'populate_current_date' );
function populate_current_date( $value ) {
$local_timestamp = GFCommon::get_local_timestamp( time() );

return date_i18n( 'm/d/Y', $local_timestamp, true );
}

Populate previous value
The following example shows how a field can be populated with the previous value submitted for the current form and field by the logged in user. In this case the dynamic population parameter name setting on the field is set to 「previous_value」.
add_filter( 'gform_field_value_previous_value', 'populate_previous_value', 10, 2 );
function populate_previous_value( $value, $field ) {
$entries = GFAPI::get_entries(
$field->formId,
array(
'field_filters' => array(
array(
'key' => 'created_by',
'value' => get_current_user_id(),
),
),
),
array(),
array( 'page_size' => 1 )
);

return rgars( $entries, '0/' . $field->id );
}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFFormsModel::get_parameter_value() in forms_model.php

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注