1

I have a vocabulary with states and cities, where city are children of state:

Taxonomy

I have an exposed filter with depth. How can I show parent term after child? For example, like this:

Exposed filters

I'm confused because the array has no relation between parent and child. For example, this is the form array select box:

{
#type: "select",
#options: {
0: {
option: {
17: "Aragon"
}
},
1: {
option: {
21: "-Barbastro"
}
},
2: {
option: {
20: "-Calatayud"
}
},
3: {
option: {
19: "-Zaragoza"
}
},
4: {
option: {
16: "Cataluña"
}
},
5: {
option: {
22: "-Barcelona"
}
},
6: {
option: {
24: "-Girona"
}
},
7: {
option: {
23: "-Tarragona"
}
},
8: {
option: {
18: "Valencia"
}
},
9: {
option: {
26: "-Alicante"
}
},
10: {
option: {
27: "-Elche"
}
},
11: {
option: {
25: "-Valencia"
}
},
All: "- Any -"
},
#size: "",
#default_value: "All"
},
2

The exposed taxonomy filter select box form doesn't have any hierarchy built into the select form array (e.g. using subarray to present optgroup elements). Rather, its labels are indented to present the hierarchy on each select option.

To alter the labels, you can utilize hook_form_views_exposed_form_alter() to alter the selectbox array structure with some string manipulate logic. For example:

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  $parent = '';
  $options = $form['tid']['#options'];
  foreach ($options as $index => $option) {
    if (is_object($option)) {
      $label = reset($option->option);
      $key = key($option->option);
      if (substr($label, 0, 1) != '-') {
        $parent = $label;
      }
      else {
        $options[$index]->option[$key] = $options[$index]->option[$key] . " ($parent)";
      }
    }
  }
}
  • Tank you so much! work perfect for me. – Mark Bg Aug 24 '16 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.