Skip to content

Instantly share code, notes, and snippets.

@danielkellyio
Created July 28, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkellyio/4b26130050719c8d235d50e5a28a080d to your computer and use it in GitHub Desktop.
Save danielkellyio/4b26130050719c8d235d50e5a28a080d to your computer and use it in GitHub Desktop.
custom gf fields
<?php
/*
* Recruiting Specific Field Buttons
*
* @author - Daniel Kelly
* @date - August 15, 2016
* @notes-
* https://www.gravityhelp.com/documentation/article/gform_add_field_buttons/
* button will not display until action "gform_editor_js_set_default_values" is set
*/
/*******************************
* Recruiting Group and Buttons
*******************************/
$GLOBALS['rr_recruiting_field_types'] = ["experience", "driver_type", "cdl"];
add_filter("gform_add_field_buttons", "add_recruiting_buttons");
function add_recruiting_buttons( $field_groups )
{
$field_groups[] = [
"name"=> 'recruiting_fields',
'label'=> 'Recruiting Fields',
'fields'=>[
[
'class' => 'button',
'data-type' => 'experience',
'value' => __('Experience', "gravityforms"),
'onclick' => "StartAddField('experience');",
'conditional_logic'=> true
],
[
'class' => 'button',
'data-type' => 'cdl',
'value' => __('CDL', "gravityforms"),
'onclick' => "StartAddField('cdl');"
],
[
'class' => 'button',
'data-type' => 'driver_type',
'value' => __('Driver Type', "gravityforms"),
'onclick' => "StartAddField('driver_type');"
],
],
];
return $field_groups;
}
/*******************************
* Add Titles to Recruiting Fields
*******************************/
add_filter( 'gform_field_type_title', 'recruiting_fields_titles', 10, 2 );
function recruiting_fields_titles( $title, $field_type ) {
switch($field_type){
case 'experience' :
return 'Experience';
break;
case 'cdl' :
return 'CDL';
break;
case 'driver_type' :
return 'Driver Type';
break;
default:
return $title;
}
}
/*******************************
* Field Inputs for Recruiting Fields
*******************************/
add_action( "gform_field_input" , "recruiting_field_inputs", 10, 5 );
function recruiting_field_inputs ( $input, $field, $value, $lead_id, $form_id ){
if ( in_array($field['type'], $GLOBALS['rr_recruiting_field_types'])) {
$tabindex = GFCommon::get_tabindex();
$css = isset( $field["cssClass"] ) ? $field["cssClass"] : "";
$options = @implode("", array_map(function($choice){
return "<option value='" . $choice['value'] . "'>" .$choice['text'] . "</option>";
}, $field['choices']));
$disabled = (is_admin()) ? "disabled" : "";
return "<div class='ginput_container ginput_container_select'><select name='input_" . $field["id"] . "' id='input_" . $form_id . "_". $field['id'] . "' class='gfield_select medium " . $field["type"] . " " . esc_attr( $css ) . " " . $field['size'] . "' $tabindex aria-invalid='false' value='" . esc_html($value) . "' " . $disabled . ">$options</select></div>";
}
return $input;
}
/*******************************
* Recruiting Fields Settings
*******************************/
add_action( "gform_editor_js", "recruiting_fields_settings" );
function recruiting_fields_settings(){
?>
<script>
(function($){
var recruitingFieldTypes = JSON.parse('<?php echo json_encode($GLOBALS['rr_recruiting_field_types']) ?>');
//Add all text input settings to the 'smart_zip' settings
fieldSettings['experience'] = fieldSettings['select'];
fieldSettings['cdl'] = fieldSettings['select'];
fieldSettings['driver_type'] = fieldSettings['select'];
//binding to the load field settings event to initialize the checkbox
$(document).bind("gform_load_field_settings", function(event, field, form){
});
$(document).bind('gform_field_added', function(event, form, field){
switch(field.type){
case 'experience' :
field.label = 'Years of Experience?';
field.choices = [
{
isSelected : false,
price : "",
text : 'Select One',
value : ''
},
{
isSelected : false,
price : "",
text : '1-2 Years',
value : '1-2 Years'
},
{
isSelected : false,
price : "",
text : '3-4 Years',
value : '3-4 Years'
},
{
isSelected : false,
price : "",
text : '5-8 Years',
value : '5-8 Years'
},
{
isSelected : false,
price : "",
text : '8-10 Years',
value : '8-10 Years'
},
{
isSelected : false,
price : "",
text : '10+ Years',
value : '10+ Years'
}
];
break;
case'cdl' :
field.label = 'Do you have a valid CDL?';
field.choices = [
{
isSelected : false,
price : "",
text : 'Select One',
value : ''
},
{
isSelected : false,
price : "",
text : 'Yes',
value : 'Yes'
},
{
isSelected : false,
price : "",
text : 'No',
value : 'No'
}
];
break;
case 'driver_type' :
field.label = 'Driver Type?';
field.choices = [
{
isSelected : false,
price : "",
text : 'Select One',
value : ''
},
{
isSelected : false,
price : "",
text : 'Owner Operator',
value : 'Owner Operator',
ams_id : 19
},
{
isSelected : false,
price : "",
text : 'Company Driver',
value : 'Company Driver',
ams_id : 18
},
{
isSelected : false,
price : "",
text : 'Lease Purchase',
value : 'Lease Purchase',
ams_id : 20
}
];
break;
}
var thisField = $("#field_" + field.id);
thisField.find(".gfield_label").html(field.label + '<span class="gfield_required"></span>');
//Add choices to select as options
if(recruitingFieldTypes.indexOf(field.type) > -1){
var select = thisField.find(".ginput_container .gfield_select." + field.type);
for (var i = 0; i < field.choices.length; i++) {
select.append("<option value='" + field.choices[i].value +"'>" + field.choices[i].text +"</option>");
}
}
});
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment