Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senlin/af45010ccfb44ee6c707 to your computer and use it in GitHub Desktop.
Save senlin/af45010ccfb44ee6c707 to your computer and use it in GitHub Desktop.
<?php
/**
* Gravity Wiz // Gravity Forms // Map Submitted Field Values to Another Field
*
* Preview your Gravity forms on the frontend of your website. Adds a "Live Preview" link to the Gravity Forms toolbar.
*
* Usage
*
* 1 - Enable "Allow field to be populated dynamically" option on field which should be populated.
* 2 - In the "Parameter Name" input, enter the merge tag (or merge tags) of the field whose value whould be populated into this field.
*
* Basic Fields
*
* To map a single input field (and most other non-multi-choice fields) enter: {Field Label:1}. It is useful to note that
* you do not actually need the field label portion of any merge tag. {:1} would work as well. Change the "1" to the ID of your field.
*
* Multi-input Fields (i.e. Name, Address, etc)
*
* To map the first and last name of a Name field to a single field, follow the steps above and enter {First Name:1.3} {Last Name:1.6}.
* In this example it is assumed that the name field ID is "1". The input IDs for the first and last name of this field will always be "3" and "6".
*
* # Uses
*
* - use merge tags as post tags
* - aggregate list of checked checkboxes
* - map multiple conditional fields to a single field so you can refer to the single field for the selected value
*
* @version 1.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2014 Gravity Wiz
*/
class GWMapFieldToField {
public $lead = null;
function __construct( ) {
add_filter( 'gform_pre_validation', array( $this, 'map_field_to_field' ), 11 );
}
function map_field_to_field( $form ) {
foreach( $form['fields'] as $field ) {
if( is_array( $field['inputs'] ) ) {
$inputs = $field['inputs'];
} else {
$inputs = array(
array(
'id' => $field['id'],
'name' => $field['inputName']
)
);
}
foreach( $inputs as $input ) {
$value = rgar( $input, 'name' );
if( ! $value )
continue;
$post_key = 'input_' . str_replace( '.', '_', $input['id'] );
$current_value = rgpost( $post_key );
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $input['name'], $matches, PREG_SET_ORDER );
// if there is no merge tag in inputName - OR - if there is already a value populated for this field, don't overwrite
if( empty( $matches ) )
continue;
$entry = $this->get_lead( $form );
foreach( $matches as $match ) {
list( $tag, $field_id, $input_id, $filters, $filter ) = array_pad( $match, 5, 0 );
$force = $filter === 'force';
$tag_field = RGFormsModel::get_field( $form, $field_id );
// only process replacement if there is no value OR if force filter is provided
$process_replacement = ! $current_value || $force;
if( $process_replacement && ! RGFormsModel::is_field_hidden( $form, $tag_field, array() ) ) {
$field_value = GFCommon::replace_variables( $tag, $form, $entry );
if( is_array( $field_value ) ) {
$field_value = implode( ',', array_filter( $field_value ) );
}
} else {
$field_value = '';
}
$value = trim( str_replace( $match[0], $field_value, $value ) );
}
if( $value ) {
$_POST[$post_key] = $value;
}
}
}
return $form;
}
function get_lead( $form ) {
if( ! $this->lead )
$this->lead = GFFormsModel::create_lead( $form );
return $this->lead;
}
}
new GWMapFieldToField();
@pareshmithra
Copy link

Hi Team,

I'm using this code in my gravity form to display selected category. In the form, field id 156 is the field where i'm displaying all the selected categories using parameters. Link is https://www.dropbox.com/s/yi1dk4ttqaskdoh/2018-02-16_3505.png?dl=0

Below are my field id's used in parameters.

Parameter Names:
{Karting Regular National:29.1} {Karting Club Sport: Rs 250/-:30.1} {International Karting Grade C Jr (International Karting Grade C Jr (13 years in 2018 less than 15 years on the date of issue) : Rs 4,500/-):32.1} {International Karting Grade C Restricted (International Karting Grade C Restricted (Non-geared) (14 years in 2018 and less than 15 years on the date of issue) : Rs 4,500/-):34.1} {International Karting Grade C Sr (International Karting Grade C Sr (15 years on 2018) : Rs 4500/-):33.1} {International Karting Grade B (International Karting Grade B (15 years on 2018 (Refer CIK regs)) : Rs 6000/-):36.1} {International Karting Grade A (International Karting Grade A Sr (15 years on 2018) : Rs 10000/-):35.1} {Karting Cadet (Rs 1500):37.1} {2 W Racing:43} {2 W Rally/Raid:44} {2 W Dirt/AutoX/SuperX/MotoX/SuperMoto:45} {2 W Club Sport Rally (Rs 250/-):46.1} {2 W Club Sport Others (Rs 250/-):148.1} {4W National Rally - Rs 2500:150.1} {4W National Race - Rs 2500:151.1} {4 W National Drag/AutoX/Hill Climb - Rs 2500:152.1} {4 W Club Sport Rally - Rs 400:153.1} {4 W Club Sport Others - Rs 400:154.1} {4 W International Racing:50} {4 W International Rally:51} {2 W Drag:166}

The above fields are there in the Application form and user can select multiple fields. And I'm displaying all the fields selected in field id 156. But sometimes, if the user selects 2 or more fields while applying it is not showing all the selected fields. It shows only one or two. It is not happening regularly. Sometimes it shows properly. Why is this happening. The same code I used in gravity view custom HTML field, there it displays all the selected fields.

Please help to solve this issue as this is creating huge confusion.

Regards,
Paresh Mithra PS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment