You are here

function submenutree_node_view in Submenu Tree 7

Same name and namespace in other branches
  1. 7.2 submenutree.module \submenutree_node_view()

Implements hook_node_view().

File

./submenutree.module, line 206
Submenu Tree

Code

function submenutree_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full' && node_is_page($node) && (!empty($node->submenutree_enable) || !empty($node->siblingmenutree_enable))) {
    $mlid = 0;

    // Other modules may override which mlid to use. Use the first available value.
    foreach (module_implements('submenutree_mlid') as $module) {
      $function = $module . '_submenutree_mlid';
      if ($mlid = $function($node)) {
        break;
      }
    }

    // Else, give priority to the default menu defined by the Menu module.
    // This stanza is from menu_node_prepare()
    if (!$mlid) {

      // Give priority to the default menu
      $menu_name = variable_get('menu_parent_' . $node->type, 'main-menu:0');
      $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
        ':path' => 'node/' . $node->nid,
        ':menu_name' => $menu_name,
      ))
        ->fetchField();
    }

    // Check all menus if a link does not exist in the default menu.
    if (!$mlid) {
      $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
        ':path' => 'node/' . $node->nid,
      ))
        ->fetchField();
    }
    if ($mlid) {
      $item = menu_link_load($mlid);
      $tree = menu_tree_all_data($item['menu_name'], $item);

      // Traverse down the tree to find our node, following in_active_trial
      // This code stanza is loosely inspired by menu_set_active_trail()
      $my_tree = FALSE;
      $parent_tree = FALSE;
      list($key, $curr) = each($tree);
      while ($curr) {
        if ($curr['link']['href'] == $item['href']) {
          $my_tree = $curr['below'];
          $parent_tree = $tree;

          // Clear the children in the parent_tree if it is not an expanded menu item
          if (empty($parent_tree[$key]['link']['expanded'])) {
            $parent_tree[$key]['below'] = array();
          }
          $curr = FALSE;
        }
        else {

          // Move to the child link if it's in the active trail.
          if ($curr['below'] && $curr['link']['in_active_trail']) {
            $tree = $curr['below'];
          }
          list($key, $curr) = each($tree);
        }
      }

      // Sanity check that we did find something
      if ($my_tree && $node->submenutree_enable) {
        _submenutree_menutree_view($node, 'submenutree', $my_tree);
      }
      if ($parent_tree && $node->siblingmenutree_enable) {
        _submenutree_menutree_view($node, 'siblingmenutree', $parent_tree);
      }
    }
  }
}