Index: simplemenu.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplemenu/simplemenu.module,v
retrieving revision 1.9.2.11.2.5
diff -u -p -r1.9.2.11.2.5 simplemenu.module
--- simplemenu.module	6 Dec 2008 17:12:22 -0000	1.9.2.11.2.5
+++ simplemenu.module	7 Dec 2008 18:33:43 -0000
@@ -61,29 +61,12 @@ function simplemenu_init() {
     );
 
     drupal_add_js(array('simplemenu' => $settings), 'setting');
-  }
-}
-
-/**
- * Implementation of hook_footer().
- * 
- * This has been broken off of simplemenu_init() because simplemenu_get_menu()
- * calls simplemenu_menu_tree() which calls menu_tree_output() which has several
- * calls to theme().  This initializes the theme system too early causing hard
- * to track bugs.
- * 
- * @see http://drupal.org/node/219910
- */
-function simplemenu_footer() {
-  if(simplemenu_enabled()) {
+    
     $simplemenu = drupal_to_js(simplemenu_get_menu());
-    $path = base_path() . drupal_get_path('module', 'simplemenu');
-
-    $output = "<script type=\"text/javascript\">var simplemenu = $simplemenu;</script>\n";
-    $output .= "<script type=\"text/javascript\" src=\"$path/simplemenu.js\"></script>\n";
-    $output .= "<script type=\"text/javascript\" src=\"$path/superfish.js\"></script>\n";
     
-    return $output;
+    drupal_add_js('var simplemenu = '. drupal_to_js(simplemenu_get_menu()) .';', 'inline');
+    drupal_add_js($path .'/simplemenu.js');
+    drupal_add_js($path .'/superfish.js');    
   }
 }
 
@@ -224,20 +207,18 @@ function simplemenu_get_menu() {
   // if a user turned off menu module but SimpleMenu was previously set
   // reset variable so a menu appears
   $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
-  $menu = simplemenu_menu_tree($menu_name);
+  $tree = simplemenu_menu_tree($menu_name);
 
-  if (!$menu) {
-    $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
-  }
+  // allow other modules to alter the tree
+  drupal_alter('simplemenu_tree', $tree);
   
-  // This is ugly, I know, but it is the only way I can see to get the additional 
-  // links inside the <ul> tags
-  if($devel = simplemenu_get_devel()) {
-    $pos = strpos($menu, '>') + 1;
-    $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
+  // if the tree is still empty, display a link to the settings.
+  if (!$tree) {
+    $tree[] = l(t('No menu items found. Try a different menu as the default.'), 'admin/settings/simplemenu');
   }
-  
-  $output .= $menu;
+
+  // render for output
+  $output = simplemenu_tree_output($tree);
 
   return $output;
 }
@@ -246,19 +227,97 @@ function simplemenu_get_menu() {
  * Custom implementation of menu_tree().
  * We want to retrieve the entire menu structure for a given menu,
  * regardless of whether or not the menu item is expanded or not.
+ * 
+ * @param $menu_name
+ *   The name of the menu.
+ * @return
+ *   A tree array ready for simplemenu_tree_output(). 
  */
-
 function simplemenu_menu_tree($menu_name = 'navigation:0') {
   static $menu_output = array();
 
   if (!isset($menu_output[$menu_name])) {  
     $tree = simplemenu_tree_all_data($menu_name);
-    $menu_output[$menu_name] = menu_tree_output($tree);
+    $menu_output[$menu_name] = simplemenu_compact_menu_tree($tree);
   }
+  
   return $menu_output[$menu_name];  
 }
 
 /**
+ * Recursive function to compact the menu tree into our item_list style tree
+ *
+ * @param $tree
+ *   A menu tree array as returned from menu_tree_all_data().
+ * @return 
+ *   A tree array ready for simplemenu_tree_output().
+ */
+function simplemenu_compact_menu_tree($tree) {
+  $items = array();
+  foreach($tree as $data) {
+    if(!$data['link']['hidden']) {
+      $link = $data['link'];
+      if (empty($link['localized_options'])) {
+        $link['localized_options'] = array();
+      }
+
+      $link = l($link['title'], $link['href'], $link['localized_options']);
+      if($data['below']) {
+        $items[] = array(
+          'data' => $link,
+          'children' => simplemenu_compact_menu_tree($data['below']),
+        );
+      }
+      else {
+        $items[] = $link;
+      }
+    }
+  }
+  return $items;
+}
+
+/**
+ * Render the necessary markup from the tree array. This is largely based on
+ * theme_item_list(), without the extra markup.
+ * 
+ * @param $tree
+ *   Array of items as expected in theme_item_list().
+ */
+function simplemenu_tree_output($items, $attributes = array()) {
+  if (!empty($items)) {
+    $output .= '<ul'. drupal_attributes($attributes) .'>';
+    $num_items = count($items);
+    foreach ($items as $i => $item) {
+      $attributes = array();
+      $children = array();
+      if (is_array($item)) {
+        foreach ($item as $key => $value) {
+          if ($key == 'data') {
+            $data = $value;
+          }
+          elseif ($key == 'children') {
+            $children = $value;
+          }
+          else {
+            $attributes[$key] = $value;
+          }
+        }
+      }
+      else {
+        $data = $item;
+      }
+      if (count($children) > 0) {
+        $data .= simplemenu_tree_output($children); // Render nested list
+        $attributes['class'] = empty($attributes['class']) ? 'expanded' : ($attributes['class'] .' expanded');
+      }
+      $output .= '<li'. drupal_attributes($attributes) .'>'. $data ."</li>\n";
+    }
+    $output .= "</ul>\n";
+  }
+  return $output;
+}
+
+/**
  * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  * (which can be *any* menu item, not just the root of a custom menu).
  *
@@ -320,21 +379,17 @@ function simplemenu_tree_all_data($root_
 }
 
 /**
- * Return a list of devel module links if the module is enabled
- * and the user has access to this module.
+ * Implementation of hook_simplemenu_tree_alter().
  */
-function simplemenu_get_devel() {
-  $output = '';
-
-  if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
-    if (user_access('access devel information')) {
-      $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
-      $output .= simplemenu_menu_tree('devel');
-      $output .= '</li>';
-    }
+function simplemenu_simplemenu_tree_alter(&$tree) {
+  if (variable_get('simplemenu_devel', 0) && module_exists('devel') && user_access('access devel information')) {
+    $devel = array(
+      'data' => l(t('Devel module'), 'admin/settings/devel'),
+      'children' => simplemenu_menu_tree('devel:0'),
+    );
+    
+    array_unshift($tree, $devel);
   }
-  
-  return $output;
 }
 
 /**
