Sorry for my ignorance, but I do not understand forms API. I've looked at documentation but still don't get it. Can someone simply tell me in what file to insert this snippet of code? (I'm using Drupal 5.2 btw)
Also, just to make sure, if I understand it well, this code allows you to put parent categories in < optgroup > ?
Anyone? I figured that I have to create my own module, with this code included in it.... I don't have any experience with creating modules though. Anyone willing to help? Thanks.
Solved
<?php
// ..._form_alter...
$term_list = taxonomy_form($vid);
$options = array();
$tree=taxonomy_get_tree($vid);
$top_parents=array();
foreach($tree as $id => $term){
if($term->parents[0] == 0) $top_parents[]=$term->tid;
}
$curr_group='';
foreach( $term_list["#options"] as $key => $text){
if( in_array($key, $top_parents) ){
$options[$text]=array();
$curr_group=$text;
}else{
$options[$curr_group][$key]=$text;
}
}
$term_list['#options']=$options;
$form['taxonomy'] = $term_list;
?>
optgroup in Drupal 5.0
Seems that "option" structure changed in version 5.
So, the code becomes a little more complex:
<?php
function module_form_alter(){
//......
$term_list = taxonomy_form($vid);
$term_list['#weight'] = 1;
$term_list['#title'] = "Title";
$term_list['#description'] = "Descr";
$term_list['#attributes'] = array('size' => '30');
$term_list['#required']=1;
$options = array();
$tree=taxonomy_get_tree($vid);
$mains=array();
foreach($tree as $id => $term){
if($term->parents[0] == 0) $mains[]=$term->tid;
}
$curr_group='';
foreach( $term_list["#options"] as $id => $option){
$opt = _get_option($option); $key=$opt[0]; $text=$opt[1];
if( in_array($key, $mains) ){
$curr_group=$text;
$options[$curr_group]=array();
}else{
$options[$curr_group][]=$option;
}
}
$term_list['#options']=$options;
return $term_list;
}
function _get_option($option){
foreach($option->option as $key=>$value){
if(is_numeric($key)) return array($key, $value);
}
}
?>
Sorry for my ignorance, but
Sorry for my ignorance, but I do not understand forms API. I've looked at documentation but still don't get it. Can someone simply tell me in what file to insert this snippet of code? (I'm using Drupal 5.2 btw)
Also, just to make sure, if I understand it well, this code allows you to put parent categories in < optgroup > ?
Thanks.
Anyone? I figured that I
Anyone? I figured that I have to create my own module, with this code included in it.... I don't have any experience with creating modules though. Anyone willing to help? Thanks.
I think you create a file
I think you create a file called optgroup.module with the code posted above and another file called optgroup.info with :
; $Id: optgroup.info,v 1.0
name = Optgroup
description = "your description"
; version
version = "5.x"
Does it works for Drupal 6.12
Does it works for Drupal 6.12 .... ??
Drupal 6.x code
<?php
if (isset($form['taxonomy'][$vid])) {
$tree = taxonomy_get_tree($vid);
$parents = array();
$options[''] = $form['taxonomy'][$vid]['#options'][''];
foreach ($tree as $term) {
$obj = new stdClass();
$obj->option[$term->tid] = $term->name;
if($ptid = $term->parents[0]) {
$options[$parents[$ptid]][] = $obj;
} else {
$parents[$term->tid] = $term->name;
$options[$term->name] = array();
}
}
$form['taxonomy'][$vid]['#options'] = $options;
}
?>