Create New select field (Drop Down)

Hello Everyone,
I tried to create new select field then when i add it to my website the input still disabled. Can someone helps how can I solve this problem. Thank you.

This is my code:

`// Add a custom field button to the advanced to the field editor
add_filter('gform_add_field_buttons', 'add_field');
function add_field($field_groups)

{
  foreach ($field_groups as &$group) {
    if ($group['name'] == 'standard_fields') {
      $group['fields'][] = array (
        'class'     => 'button',
        'value'     => __('choices_field', 'gravityform'),
        'onclick'   => "StartAddField('abdo');",
        //'data-type' => 'choices_field'
      );
      break;
    }
  }
  return $field_groups;
}

// Adds title to GF custom field

add_filter( 'gform_field_type_title' , 'add_title' );
function add_title( $title ) {
if ( $title== 'abdo' )
return __( 'New_dropdown' , 'gravityforms' );
}

// Adds the input area to the external side
add_action( "gform_field_input" , "wps_abdo_field_input", 10, 5 );
function wps_abdo_field_input ( $input, $field, $value, $lead_id, $form_id ){

if ( $field["type"] == "abdo" ) {

//$input_name = $form_id .'_' . $field["id"];
$tabindex = GFCommon::get_tabindex();
$get_choices=GFCommon::get_select_choices( $field, $value='', $support_placeholders = true );
$val = RGFormsModel::get_choice_text( $field, $value );
//$css = isset( $field['cssClass'] ) ?: $field['cssClass'] ;
return sprintf("<div class='ginput_container ginput_container_select'><select name='input_%s' id='%s' class='%s gfield_select' $tabindex disabled='disabled' aria-invalid='False'> %s
%s</select></div>", $field["id"], 'input_'.$field['id'] , $field['size'],$get_choices,$val);

}

return $input;
}


// Now we execute some javascript technicalitites for the field to load correctly
add_action( "gform_editor_js", "wps_gform_editor_js" );
function wps_gform_editor_js(){
?>

<script type='text/javascript'>

jQuery(document).ready(function($) {

// from forms.js; can add custom "abdo_setting" as well
fieldSettings["abdo"] = ".label_placement_setting,.label_setting, .description_setting, .admin_label_setting, .size_setting, .default_value_setting, .placeholder_setting, .error_message_setting, .css_class_setting, .choices_setting, .duplicate_setting, .enable_enhanced_ui_setting, .rules_setting, .visibility_setting, .conditional_logic_field_setting,.prepopulate_field_setting, .abdo_setting"; //this will show all the fields of the Paragraph Text field minus a couple that I didn't want to appear.

//binding to the load field settings event to initialize the checkbox
$(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_abdo").attr("checked", field["field_abdo"] == true);
$("#field_abdo_value").val(field["abdo"]);
});
});

</script>
<?php
}


add_action( 'gform_editor_js_set_default_values' , 'wdm_set_default_values' );
function wdm_set_default_values()
{
  // Define the type of Gravity Forms field you are creating
  ?>
  case 'abdo' : field.inputType = 'checkbox';
  field.label = <?php echo json_encode(esc_html__('Custom Field Title', 'gravityforms')); ?>;
  if (!field.label)
    field.label = <?php echo json_encode(esc_html__('Custom Field Title', 'gravityforms')); ?>;
  if (!field.choices)
    field.choices = new Array(new Choice(<?php echo json_encode(esc_html__('First Choice', 'gravityforms')); ?>), new Choice(<?php echo json_encode(esc_html__('Second Choice', 'gravityforms')); ?>), new Choice(<?php echo json_encode(esc_html__('Third Choice', 'gravityforms')); ?>));
  field.inputs = new Array();
  for (var i = 1; i <= field.choices.length; i++) {
    field.inputs.push(new Input(field.id + (i / 10), field.choices[i - 1].text));
  }
  break;
  <?php
}`

this a Picture to show difference between original and the new field:

I don’t recommend using those filters. With Gravity Forms 1.9 and greater the recommended way to implement a new field type is to extend the GF_Field class. See the following articles:



1 Like