I have taxonomy terms with parent/child hierarchies (1 level deep). On a node edit page the terms are shown in a select box where I can select multiple terms.
What I'm trying to do is convert the parent terms into optgroups so they cannot get selected and the children are nicely grouped. This post shows how to structure the form array.
Basically, the array needs to get arranged like this:
$form['field_disc_topic']['und']['#options'] = array (
'parent1' => array(
3 => 'child1',
4 => 'child2',
5 => 'child3',
),
26 => 'parent2' => array(
8 => 'child1',
9 => 'child2',
),
);
This is the code I started with. A parent term has depth=0, children have depth=1. I can't figure out to how build the array:
$tax = taxonomy_get_tree(2);
for ($i = 0; $i < count($tax); ++$i) {
debug( $tax[$i]->tid);
debug($tax[$i]->name);
debug($tax[$i]->depth);
}
Can anyone solve this? Or is there an easier way to do this?