### Eclipse Workspace Patch 1.0
#P menu_block
Index: menu_block.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/menu_block/menu_block.module,v
retrieving revision 1.69.2.2
diff -u -r1.69.2.2 menu_block.module
--- menu_block.module	28 Sep 2010 20:18:54 -0000	1.69.2.2
+++ menu_block.module	24 Oct 2010 08:08:25 -0000
@@ -141,6 +141,33 @@
 }
 
 /**
+ * Determines the active menu based on the current page path.
+ *
+ * If any of the available menus contain a link to the current page, this
+ * function returns the name of the first menu that links to that page.
+ *
+ * @return
+ *   The name of the active menu or FALSE if none of the available menus link to
+ *   the current page.
+ */
+function menu_block_get_active_menu() {
+  // Retrieve the list of available menus.
+  $menu_order = variable_get('menu_block_menu_order', array('primary-links' => '', 'secondary-links' => ''));
+
+  // Retrieve all the menus containing a link to the current page.
+  $result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = '%s'", $_GET['q'] ? $_GET['q'] : '<front>');
+  while ($item = db_fetch_array($result)) {
+    // Check if the menu is in the list of available menus.
+    if (isset($menu_order[$item['menu_name']])) {
+      // Mark the menu.
+      $menu_order[$item['menu_name']] = MENU_TREE__CURRENT_PAGE_MENU;
+    }
+  }
+  // Find the first marked menu.
+  return array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);
+}
+
+/**
  * Implements hook_block().
  */
 function menu_block_block($op = 'list', $delta = NULL, $edit = NULL) {
@@ -235,24 +262,15 @@
 function menu_tree_build($config) {
   // Retrieve the active menu item from the database.
   if ($config['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {
-    // Retrieve the list of available menus.
-    $menu_order = variable_get('menu_block_menu_order', array('primary-links' => '', 'secondary-links' => ''));
+    $active_menu = menu_block_get_active_menu();
 
-    // Retrieve all the menus containing a link to the current page.
-    $result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = '%s'", $_GET['q'] ? $_GET['q'] : '<front>');
-    while ($item = db_fetch_array($result)) {
-      // Check if the menu is in the list of available menus.
-      if (isset($menu_order[$item['menu_name']])) {
-        // Mark the menu.
-        $menu_order[$item['menu_name']] = MENU_TREE__CURRENT_PAGE_MENU;
-      }
+    if ($active_menu) {
+      $config['menu_name'] = $active_menu;
+      $config['parent_mlid'] = 0;
     }
-    // Find the first marked menu.
-    $config['menu_name'] = array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);
-    $config['parent_mlid'] = 0;
-
-    // If no menu link was found, don't display the block.
-    if (empty($config['menu_name'])) {
+    else {
+      // If no active menu could be determined based on the current page path,
+      // don't display the block.
       return array();
     }
   }
