--- taxonomy_menu.module.orig	Wed Jan  5 19:19:10 2005
+++ taxonomy_menu.module	Wed Jan  5 20:36:21 2005
@@ -19,38 +19,21 @@
 function taxonomy_menu_menu($may_cache) {
   $items = array();
 
-  if ($may_cache) {
-    $access = user_access('access content');
+  $is_dynamic = variable_get('taxonomy_menu_count_nodes', 0);
 
+  if ($may_cache && $is_dynamic == 0) {
+    $access = user_access('access content');
     $items[] = array('path' => 'taxonomy_menu', 'title' => t('Taxonomy'),
       'callback' => 'taxonomy_menu_page', 'access' => $access,
       'type' => MENU_CALLBACK);
-
-    foreach (taxonomy_get_vocabularies() as $vocabulary) {
-      if (variable_get('taxonomy_menu_show_'. $vocabulary->vid, 1)) {
-        $path = 'taxonomy_menu/'. $vocabulary->vid;
-        $items[] = array('path' => $path, 'title' => t($vocabulary->name),
-            'weight' => $vocabulary->weight);
-
-        $tree = taxonomy_get_tree($vocabulary->vid);
-        $old_depth = -1;
-        $old_path = $path;
-
-        foreach ($tree as $term) {
-          if ($term->depth <= $old_depth) {
-            $slashes_to_remove = $old_depth - $term->depth + 1;
-            for ($i = 0; $i < $slashes_to_remove; $i++) {
-              $old_path = substr($old_path, 0, strrpos($old_path, '/'));
-            }
-          }
-          $path = $old_path .'/'. $term->tid;
-          $old_depth = $term->depth;
-          $old_path = $path;
-          $items[] = array('path' => $path, 'title' => t($term->name),
-            'weight' => $term->weight);
-        }
-      }
-    }
+    _generate_menus($items, NULL);
+  }
+  else {
+    $access = user_access('access content');
+    $items[] = array('path' => 'taxonomy_menu', 'title' => t('Taxonomy'),
+      'callback' => 'taxonomy_menu_page', 'access' => $access,
+      'type' => MENU_CALLBACK);
+    _generate_menus($items, 1);
   }
 
   return $items;
@@ -61,6 +44,7 @@
  */
 function taxonomy_menu_settings() {
   $form .= form_checkbox(t('Display descendants'), 'taxonomy_menu_display_descendants', 1, variable_get('taxonomy_menu_display_descendants', 1), t('If checked, then when a term is selected all nodes belonging to subterms are also displayed.'));
+  $form .= form_checkbox(t('Display node count'), 'taxonomy_menu_count_nodes', 1, variable_get('taxonomy_menu_count_nodes', 0), t('If checked, the number of nodes within a particular term are displayed in parentheses.'));
 
   foreach (taxonomy_get_vocabularies() as $vocabulary) {
     $form .= form_checkbox(t('Show "%vocab" in menu', array('%vocab' => t($vocabulary->name))), 'taxonomy_menu_show_' . $vocabulary->vid, 1, variable_get('taxonomy_menu_show_' . $vocabulary->vid, 1));
@@ -125,6 +109,40 @@
         }
       }
       break;
+  }
+}
+
+/**
+ * Generate a list of menu items, with or without counts.
+ *
+ * @param $count_nodes
+ *   Determines whether number of nodes per tid is displayed.
+ */
+function _generate_menus(&$items, $count_nodes = NULL) {
+  foreach (taxonomy_get_vocabularies() as $vocabulary) {
+    if (variable_get('taxonomy_menu_show_'. $vocabulary->vid, 1)) {
+      $path = 'taxonomy_menu/'. $vocabulary->vid;
+      $items[] = array('path' => $path, 'title' => t($vocabulary->name), 'weight' => $vocabulary->weight);
+
+      $tree = taxonomy_get_tree($vocabulary->vid);
+      $old_depth = -1;
+      $old_path = $path;
+
+      foreach ($tree as $term) {
+        if ($count_nodes) { $node_count = ' (' . taxonomy_term_count_nodes($term->tid) . ')'; }
+
+        if ($term->depth <= $old_depth) {
+          $slashes_to_remove = $old_depth - $term->depth + 1;
+          for ($i = 0; $i < $slashes_to_remove; $i++) {
+            $old_path = substr($old_path, 0, strrpos($old_path, '/'));
+          }
+        }
+        $path = $old_path .'/'. $term->tid;
+        $old_depth = $term->depth;
+        $old_path = $path;
+        $items[] = array('path' => $path, 'title' => t($term->name) . ($node_count ? $node_count : ''), 'weight' => $term->weight);
+      }
+    }
   }
 }
 
