You are here

function submenutree_nodeapi_view in Submenu Tree 6

Same name and namespace in other branches
  1. 5 submenutree.module \submenutree_nodeapi_view()
1 call to submenutree_nodeapi_view()
submenutree_nodeapi in ./submenutree.module
Implementation of hook_nodeapi().

File

./submenutree.module, line 221

Code

function submenutree_nodeapi_view(&$node, $op, $teaser, $page) {
  if ($teaser == false && $page == true && ($node->submenutree_enable || $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.
    if (!$mlid) {
      $menu_name = variable_get('menu_default_node_menu', 'primary-links');
      $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s' AND module = 'menu' ORDER BY mlid ASC", $node->nid, $menu_name, 0, 1));
    }

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

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