Have a situation where users will need to select A Goods Sub-Type from a Parent Goods Type. Whilst I can use this plugin to display the hierarchy in one dropdown: http://wordpress.org/extend/plugins/gravity-forms-custom-post-types/ The list of goods sub types listed under goods types is very long and really should be displayed with 2 cascading dropdowns.
Trying to implement a work around by creating a dropdown that filters just the parent taxonomy terms. Then when a user has selected a parent, one of several hidden child dropdowns will display allowing selection of the goods subtype.
Managed to get the following code to return the Child Values of a Taxonomy Term in a dropdown:-
//Adds a filter to form id 1. Replace 1 with your actual form id - CHILD TERMS FOR TRUCKS
add_filter( 'gform_pre_render_1', 'populate_dropdown' );
function populate_dropdown($form){
//Grab all Terms Associated with a Specific Taxonomy;
global $post;
$termID = 4;
$taxonomyName = "goodscategories";
$termchildren = get_term_children( $termID, $taxonomyName );
//Creating drop down item array.
$items = array();
//Adding initial blank value.
$items[] = array("text" => "", "value" => "");
//Adding term names to the items array
//foreach($terms as $term){
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$is_selected = $term ->name == "testing" ? true : false;
$items[] = array("value" => $term ->name, "text" => $term ->name, "isSelected"=> $is_selected);
}
//Adding items to field id 9. Replace 9 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 9){
$field["type"] = "select";
$field["choices"] = $items;
}
return $form;
}
Tried this code for parent dropdown, but returns no values in the dropdown:-
/Adds a filter to form id 1. Replace 1 with your actual form id - PARENT TERMS ONLY
add_filter( 'gform_pre_render_1', 'populate_dropdown_parents' );
function populate_dropdown_parents($form){
//Grab all Terms Associated with a Specific Taxonomy;
global $post;
$terms = get_terms("goodscategories", 'parent=0');
//Creating drop down item array.
$items = array();
//Adding initial blank value.
$items[] = array("text" => "", "value" => "");
//Adding term names to the items array
foreach($terms as $term){
$is_selected = $term->name == "testing" ? true : false;
$items[] = array("value" => $term->name, "text" => $term->name, "isSelected"=> $is_selected);
}
//Adding items to field id 10. Replace 10 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 10){
$field["type"] = "select";
$field["choices"] = $items;
}
return $form;
}
Any help would be greatly welcome.