From 44a8be180dff6a7caae3912c62d4b25e9e7dc122 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Mon, 19 Sep 2011 17:00:44 +0000
Subject: [PATCH] [gh-206] Merging from testing into staging

git-svn-id: file:///tmp/wdn_thm_drupal/branches/drupal-7.x/staging@1058 20a16fea-79d4-4915-8869-1ea9d5ebf173
---
 sites/all/modules/unl/unl.module | 110 ++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 1 deletion(-)

diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module
index 984d2ba8..c3d2bfe7 100644
--- a/sites/all/modules/unl/unl.module
+++ b/sites/all/modules/unl/unl.module
@@ -12,12 +12,22 @@ function unl_html_head_alter(&$head_elements) {
       return;
     }
   }
+  
+  // If we are in a drilled down menu, change the home link to the drilled down item.
+  $current_menu_link = _unl_get_current_menu_link();
+  if ($current_menu_link && $current_menu_link->depth > 1) {
+    $home_path = drupal_get_path_alias($current_menu_link->link_path);
+  }
+  else {
+    $home_path = '<front>';
+  }
+  
   // ...otherwise add a <link rel="home"> tag with the front page as the href attribute
   $element = array(
     '#tag' => 'link',
     '#attributes' => array(
       'rel' => 'home',
-      'href' => url('<front>', array('absolute' => TRUE)),
+      'href' => url($home_path, array('absolute' => TRUE)),
     ),
     '#type' => 'html_tag'
   );
@@ -1038,3 +1048,101 @@ function _unl_imce_file_browser_access() {
   $profile = imce_user_profile($user);
   return $profile['usertab'];
 }
+
+/**
+ * Implements hook_block_view_alter()
+ * @param array $data
+ * @param stdClass $block
+ */
+function unl_block_view_alter(&$data, $block) {
+  if ($block->module == 'system' && $block->delta == 'main-menu') {
+    return unl_block_view_system_main_menu_alter($data, $block);
+  }
+}
+
+/**
+ * Tries to implement hook_block_view_MODULE_DELTA_alter, but since the delta contains a -,
+ * this is actually called from unl_block_view_alter() for now.
+ * Used to determin if a "sub-menu" should be used instead of the normal menu.
+ * @param array $data
+ * @param stdClass $block
+ */
+function unl_block_view_system_main_menu_alter(&$data, $block) {
+  $current_menu_link = _unl_get_current_menu_link();
+  $submenu = _unl_get_current_submenu($data['content'], $current_menu_link->mlid);
+  if ($submenu && $submenu['#original_link']['depth'] > 1) {
+    $data['content'] = $submenu['#below'];
+  }
+  $data['content'] = _unl_limit_menu_depth($data['content'], 2);
+}
+
+/**
+ * Return the mlid of the currently selected menu item.
+ * If the current page has no menu item, use return the mlid of its parent instead.
+ */
+function _unl_get_current_menu_link() {
+  $result = db_select('menu_links')
+    ->fields('menu_links')
+    ->condition('menu_name', 'main-menu')
+    ->condition('link_path', current_path())
+    ->execute()
+    ->fetch();
+    
+  if (!$result) {
+    return FALSE;
+  }
+  
+  while (($result->hidden || $result->depth % 2 !== 0 || !$result->has_children) && $result->depth > 1) {
+    $result = db_select('menu_links')
+      ->fields('menu_links')
+      ->condition('menu_name', 'main-menu')
+      ->condition('mlid', $result->plid)
+      ->execute()
+      ->fetch();
+  }
+  
+  return $result;
+}
+
+/**
+ * Find the the submenu we are currently "drilled-down" to.
+ * @param array $menu_links
+ * @param int $current_mlid
+ */
+function _unl_get_current_submenu($menu_links, $current_mlid) {
+  foreach (element_children($menu_links) as $index) {
+    $menu_item = $menu_links[$index];
+    if ($menu_item['#original_link']['mlid'] == $current_mlid) {
+      return $menu_item;
+    }
+    $sub_menu = _unl_get_current_submenu($menu_item['#below'], $current_mlid);
+    if ($sub_menu) {
+      return $sub_menu;
+    }
+  }
+  return FALSE;
+}
+
+/**
+ * Remove any menu items that are more than $depth levels below the current root.
+ * @param array $menu_links
+ * @param int $depth
+ */
+function _unl_limit_menu_depth($menu_links, $depth) {
+  if ($depth == 0) {
+    return array();
+  }
+  
+  foreach (element_children($menu_links) as $index) {
+    $menu_links[$index]['#below'] = _unl_limit_menu_depth($menu_links[$index]['#below'], $depth - 1);
+  }
+  
+  return $menu_links;
+}
+
+
+
+
+
+
+
-- 
GitLab