gform_insert_bulk_choices_choice

Description

The gform_insert_bulk_choices_choice filter allows each choice to be overridden as it is added to the field using the Bulk Add Predefined Choices UI.

This filter is generally used in combination with gform_load_bulk_choices_choice, and is useful for parsing a unique text pattern (e.g., Label|Value|Other) and adding the additional data to the resulting Choice object.

Usage

The filter which would run for all forms and choice based fields that support the Bulk Add Predefined Choices UI would be used like so:

gform.addFilter( 'gform_insert_bulk_choices_choice', function( choiceObj, choice, field ) {
    // do stuff

    return choiceObj;
} );

Parameters

  • choiceObj Javascript Object

    The object representing the choice currently being added to the field using the Bulk Add Predefined Choices UI e.g.

    {
      "text": "One",
      "value": "1",
      "isSelected": false,
      "price": ""
    }
    

    This can also include custom choice properties.

  • choice string

    The string representing the current choice as a text pattern usually in the format text|value e.g.

    One|1
    
  • field Javascript Object | Field Object

    The current field.

Examples

1. Add custom choice property

This example shows how you can add a custom property to the choice object if the choice string (e.g. One|1|#first) contains your custom property value (e.g. #first).

gform.addFilter( 'gform_insert_bulk_choices_choice', function( choiceObj, choice, field ) {
    if ( choice.indexOf( '#' ) === -1 ) {
        return choice;
    }

    var parts = choice.split( '|' );
    choiceObj.value = parts[1];
    choiceObj.mycustomprop = parts[2];

    return choiceObj;
} );

Placement

This code should be placed in a JavaScript file included in the admin by your plugin.

Since

This filter was added in Gravity Forms v2.5.

Source Code

This filter is located in InsertBulkChoices() in form_editor.js.