Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active November 3, 2016 16:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richardW8k/8707910 to your computer and use it in GitHub Desktop.
Save richardW8k/8707910 to your computer and use it in GitHub Desktop.
Change the structure of Gravity Forms radio fields so it doesn't use the extra ul and li's
add_filter( 'gform_field_input', 'rw_change_radio_structure', 10, 5 );
function rw_change_radio_structure($input, $field, $value, $lead_id, $form_id){
$input_type = RGFormsModel::get_input_type($field);
if($input_type != "radio" || IS_ADMIN && RG_CURRENT_VIEW == "entry")
return $input;
$choices = "";
if(is_array($field["choices"])){
$choice_id = 0;
$count = 1;
// add "other" choice to choices if enabled
if(rgar($field, 'enableOtherChoice')) {
$other_default_value = GFCommon::get_other_choice_value();
$field["choices"][] = array('text' => $other_default_value, 'value' => 'gf_other_choice', 'isSelected' => false, 'isOtherChoice' => true);
}
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $form_id . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
$disabled_text = IS_ADMIN ? "disabled='disabled'" : "";
foreach($field["choices"] as $choice){
$id = $field["id"] . '_' . $choice_id++;
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
$checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
$tabindex = GFCommon::get_tabindex();
$label = sprintf("<label for='choice_%s'>%s</label>", $id, $choice["text"]);
$input_focus = '';
// handle "other" choice
if(rgar($choice, 'isOtherChoice')) {
$onfocus = !IS_ADMIN ? 'jQuery(this).prev("input").attr("checked", true); if(jQuery(this).val() == "' . $other_default_value . '") { jQuery(this).val(""); }' : '';
$onblur = !IS_ADMIN ? 'if(jQuery(this).val().replace(" ", "") == "") { jQuery(this).val("' . $other_default_value . '"); }' : '';
$input_focus = !IS_ADMIN ? "onfocus=\"jQuery(this).next('input').focus();\"" : "";
$value_exists = RGFormsModel::choices_value_match($field, $field["choices"], $value);
if($value == 'gf_other_choice' && rgpost("input_{$field["id"]}_other")){
$other_value = rgpost("input_{$field["id"]}_other");
} else if(!$value_exists && !empty($value)){
$other_value = $value;
$value = 'gf_other_choice';
$checked = "checked='checked'";
} else {
$other_value = $other_default_value;
}
$label = "<input name='input_{$field["id"]}_other' type='text' value='" . esc_attr($other_value) . "' onfocus='$onfocus' onblur='$onblur' $tabindex $disabled_text />";
}
$choices .= sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex %s $logic_event %s />%s", $field["id"], esc_attr($field_value), $checked, $id, $disabled_text, $input_focus, $label);
if(IS_ADMIN && $count >=5)
break;
$count++;
}
$total = sizeof($field["choices"]);
if($count < $total)
$choices .= "<div class='gchoice_total'>" . sprintf(__("%d of %d items shown. Edit field to view all", "gravityforms"), $count, $total) . "</div>";
}
$field_id = "input_{$form_id}_{$field["id"]}";
return sprintf("<div class='ginput_container'><div class='gfield_radio' id='%s'>%s</div></div>", $field_id, $choices);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment