How to output list field column values using custom merge tags

By default Gravity Forms only provides a merge tag for an entire list field. Depending on where it is used it may output a list of values or a table.

The following code adds a merge tag that outputs the comma separated values from a list field column.

For example – if we have a multi-column field with the values submitted

Column 1 Column 2 Column 3
11 12 13
21 22 23
31 32 33

And the merge tag was insert to include the first column

{Multiple column list field:2:1}

The merge tag would output

11, 21, 31

Check out the demo at : http://demo.gravitygeek.com/list-field-column-merge-tag/

If you’re not sure where to place this code I highly recommend you read How to create a WordPress plugin for your custom functions.

add_filter( 'gform_custom_merge_tags', 'add_list_column_merge_tag', 10, 4 );
add_filter( 'gform_merge_tag_filter', 'replace_list_column_merge_tag', 10, 5 );

function add_list_column_merge_tag( $merge_tags, $form_id, $fields, $element_id ) {
    foreach ( $fields as $field ) {
        if ( 'list' == $field->get_input_type() ) {
            $has_columns = is_array( $field->choices );
            if ( $has_columns ) {
                foreach( $field->choices as $key => $choice ){
                    $key++; // add 1 as the choices array is zero based
                    $merge_tags[] = array(
                        'label' => $field->label . ' - ' . $choice['text']    . ' (comma separated)',
                        'tag' => '{' . $field->label . ':' . $field->id . ':' . $key . '}'
                    );
                }
            } else {
                $merge_tags[] = array(
                    'label' => $field->label . ' (comma separated)',
                    'tag' => '{' . $field->label . ':' . $field->id . ':1}'
                );
            }
        }
    }
    return $merge_tags;
}

function replace_list_column_merge_tag( $value, $merge_tag, $modifier, $field, $raw_value ) {
    if ( $field->get_input_type() == 'list' && $merge_tag != 'all_fields' && ! empty( $modifier ) ) {

        $column_values = array();

        // count the actual number of columns
        $choices = $field->choices;
        $column_count = count( $choices );

        if ( $column_count > 1 ) {
            // subtract 1 from column number as the choices array is zero based
            $column_num = $modifier - 1;

            // get the column label so we can use that as the key to the multi-column values
            $column = rgars( $choices, "{$column_num}/text" );

            // get the list fields values from the $entry
            $values = unserialize( $raw_value );

            foreach ( $values as $value ) {
                $column_values[] = rgar( $value, $column );
            }

            $value = GFCommon::implode_non_blank( ', ', $column_values );
        } else {
             // get the list fields values from the $entry
            $values = unserialize( $raw_value );

            foreach ( $values as $value ) {
                $column_values[] = $value;
            }

            $value = GFCommon::implode_non_blank( ', ', $column_values );
        }
    }

    return $value;
}