3 issues with opt group I've found. (one feature request; can't select 0 as opt group parent!).
I talk in code better than words, so I'm post the code with the first php statement being the issue, and after >> being my solution.
Patch also attached
http://api.drupal.org/api/function/taxonomy_get_children
function content_taxonomy_allowed_values_groups($field) {
$options = array();
$parent = content_taxonomy_field_get_parent($field);
$group_parent = $field['widget']['group_parent'];
//if children in no group
$default_terms = taxonomy_get_children($parent);
taxonomy_get_children(0) gives all parent taxonomy ignoring vocabulary
>>
$default_terms = taxonomy_get_children($parent,$field['vid']);
Group terms appear as selectable
$default_terms = taxonomy_get_children($parent,$field['vid']);
foreach ($default_terms as $default_term) {
_content_taxonomy_localize_term($default_term);
$options[$default_term->tid] = check_plain($default_term->name);
}
foreach (taxonomy_get_children($group_parent) as $group) {
foreach (taxonomy_get_children($group->tid) as $term) {
_content_taxonomy_localize_term($term);
$options[$group->name][$term->tid] = check_plain($term->name);
unset($options[$term->tid]);
}
}
>>
foreach (taxonomy_get_children($group_parent) as $group) {
foreach (taxonomy_get_children($group->tid) as $term) {
_content_taxonomy_localize_term($term);
$options[$group->name][$term->tid] = check_plain($term->name);
unset($options[$term->tid]);
}
unset($options[$group->tid]); // here!
}
None option does not appear; it's never added in.
>>
<?php
function content_taxonomy_allowed_values($field) {
$options = array();
//for opt groups call different function
if (isset($field['widget']['group_parent']) && $field['widget']['group_parent'] > 0) {
$options = content_taxonomy_allowed_values_groups($field);
} else {
$depth = (is_numeric($field['depth'])) ? $field['depth'] : NULL;
$tree = taxonomy_get_tree($field['vid'], content_taxonomy_field_get_parent($field), -1, $depth);
if (is_array($tree)) {
foreach ($tree as $term) {
_content_taxonomy_localize_term($term);
if ($field['widget']['show_depth']) {
$value = str_repeat(' - ', $term->depth) . $term->name;
}
else {
$value = $term->name;
}
//do a check_plain except for selects because form api does that
$options[$term->tid] = ($field['widget']['type'] == 'content_taxonomy_select') ? $value : check_plain($value);
}
}
}
if (($field['widget']['type'] == 'content_taxonomy_select' && !$field['multiple']) || ($field['widget']['type'] == 'content_taxonomy_options' && !$field['multiple'] && !$field['required'])) {
$options = array('' => theme('content_taxonomy_options_widgets_none', $field)) + $options;
}
return $options;
}
Comment | File | Size | Author |
---|---|---|---|
content_taxonomy_optgroup.patch | 2.89 KB | hefox |
Comments
Comment #1
mh86 CreditAttribution: mh86 commentedHi!
Thanks for your patch, I committed the changes (diff)
Note that OptGroups require the latest versions of CCK, because of this fix #521002: Support for optgroups in allowed values for select elements
Comment #2
hefox CreditAttribution: hefox commentedSorry for opening it, and thanks for responding/patching so fast =)!, but what about the third issue; lack of the option when not required? Doesn't appear to be in the diff or new .module file, etc.; Doing it a different way, XD?
Comment #3
mh86 CreditAttribution: mh86 commentedyou are right, I totally forgot about the None option...
Comment #4
mh86 CreditAttribution: mh86 commentedAdd missing none options for opt-groups. so issue fixed :)