Problem

Suppose that:

  • We have a hierarchical taxonomy which has multiple parents enabled (so that one given term can have several parents) but is single selection (so only one term can be selected) ;
  • We use Taxonomy Manager to display such a tree, such that there is a default value, and that default value is one that has multiple parents.

Then Taxonomy Manager expands all the possible parents of the default value. But because this is a single selection tree it uses radio buttons, so only one of the possible terms is selected. This results in a tree which has expanded branches by default even though no items are selected under those branches.

Suggestion

Only one of the possible parents should be expanded.

How to reproduce

I did this using the Content Taxonomy module (though I believe the problem is within the realm of Taxonomy Manager - see the patch).

  1. Create a taxonomy T (as described above : multiple parents enabled ; but only single selection).
  2. Add terms to that taxonomy, such that some terms have multiple parents
  3. Create a new node type, with a field of type Content Taxonomy, using the tree widget and the taxonomy T
  4. Create a new node, select a value for the field (using one of the terms that has multiple parents)
  5. Save that node, then edit it again. All the branches of the tree that contain the value are expanded ; but only one of the possible values is selected

Patch

This patch ensures that when using radio buttons only one parent is expanded ; and it ensures that the right item (the one with the expanded parents) gets marked as checked.

Index: taxonomy_manager/taxonomy_manager.module
===================================================================
--- taxonomy_manager/taxonomy_manager.module    (revision 1269)
+++ taxonomy_manager/taxonomy_manager.module    (working copy)
@@ -333,7 +333,7 @@

   $terms_to_expand = array();
   if (count($element['#default_value']) && !$element['#expand_all']) {
-    $terms_to_expand = taxonomy_manager_tree_get_terms_to_expand($tree, $element['#default_value']);
+    $terms_to_expand = taxonomy_manager_tree_get_terms_to_expand($tree, $element['#default_value'], $element['#multiple']);
   }

   taxonomy_manager_tree_build_form($index = 0, $tree, $element['#elements'], $element, $element['#parents'], !$element['#pager'], $element['#siblings_page'], $element['#default_value'], $element['#multiple'], $terms_to_expand);
@@ -380,7 +380,7 @@
 /**
  * marks parent terms to expand if a child terms is selected by default
  */
-function taxonomy_manager_tree_get_terms_to_expand($tree, $default_values) {
+function taxonomy_manager_tree_get_terms_to_expand($tree, $default_values, $multiple) {
   $terms = array();
   foreach (array_reverse($tree) as $term) {
     if (in_array($term->tid, array_values($default_values)) || in_array($term->tid, $terms)) {
@@ -389,6 +389,9 @@
           if ($parent) {
             $terms[$parent] = $parent;
           }
+          if (!$multiple) {
+            break;
+          }
         }
       }
     }
@@ -418,10 +421,21 @@
     $this_parents = $parents;
     $this_parents[] = $term->tid;

+    $value = in_array($term->tid, $default_value) ? 1 : 0;
+    if ($value && !$multiple) {
+      // Find our direct parent
+      $newindex = $index;
+      while ($newindex >= 0 && $tree[$newindex]->depth >= $current_depth) {
+        $newindex--;
+      }
+      if ($newindex >= 0) {
+        $value = in_array($tree[$newindex]->tid, $terms_to_expand) ? 1 : 0;
+      }
+    }
     $form[$term->tid]['checkbox'] = array(
       '#type' => ($multiple) ? 'checkbox' : 'radio',
       '#title' => $term->name,
-      '#value' => in_array($term->tid, $default_value) ? 1 : 0,
+      '#value' => $value,
       '#return_value' => $term->tid,
       '#theme' => ($multiple) ? 'taxonomy_manager_tree_checkbox' : 'taxonomy_manager_tree_radio',
     );

Comments

mh86’s picture

Status: Active » Fixed

hi!
I agree, that it's better to expand only the first parent instead of every one. Thanks for providing a patch - I know this part of the taxonomy manager is a bit more complicated ;)
I committed the patch to the dev version and will be included in the next release.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.