Created
March 19, 2014 16:15
-
-
Save richardW8k/9645268 to your computer and use it in GitHub Desktop.
change Gravity Forms checkbox structure so it doesn't use the extra ul and li's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'gform_field_input', 'rw_change_checkbox_structure', 10, 5 ); | |
function rw_change_checkbox_structure($input, $field, $value, $lead_id, $form_id){ | |
$input_type = RGFormsModel::get_input_type($field); | |
if($input_type != "checkbox" || IS_ADMIN && RG_CURRENT_VIEW == "entry") | |
return $input; | |
$choices = ""; | |
if(is_array($field["choices"])){ | |
$choice_number = 1; | |
$count = 1; | |
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $form_id . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : ""; | |
$disabled_text = (IS_ADMIN && RG_CURRENT_VIEW != "entry") ? "disabled='disabled'" : ""; | |
foreach($field["choices"] as $choice){ | |
if($choice_number % 10 == 0) //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10 | |
$choice_number++; | |
$input_id = $field["id"] . '.' . $choice_number; | |
$id = $field["id"] . '_' . $choice_number++; | |
if(empty($_POST) && rgar($choice,"isSelected")){ | |
$checked = "checked='checked'"; | |
} | |
else if(is_array($value) && RGFormsModel::choice_value_match($field, $choice, rgget($input_id, $value))){ | |
$checked = "checked='checked'"; | |
} | |
else if(!is_array($value) && RGFormsModel::choice_value_match($field, $choice, $value)){ | |
$checked = "checked='checked'"; | |
} | |
else{ | |
$checked = ""; | |
} | |
$tabindex = GFCommon::get_tabindex(); | |
$choice_value = $choice["value"]; | |
if(rgget("enablePrice", $field)) | |
$choice_value .= "|" . GFCommon::to_number(rgar($choice,"price")); | |
$label = sprintf("<label for='choice_%s'>%s</label>", $id, $choice["text"]); | |
$choices .= sprintf("<input name='input_%s' type='checkbox' $logic_event value='%s' %s id='choice_%s' $tabindex %s />%s", $input_id, esc_attr($choice_value), $checked, $id, $disabled_text, $label); | |
if(IS_ADMIN && RG_CURRENT_VIEW != "entry" && $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_checkbox' 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