Hi,

We use Dynamic Persistent Menu together with the Menu Trails Module (http://drupal.org/project/menutrails)
I am expecting a problem concerning the active trail of a node wich is not in the menu itself (thats we use Menutrails)

On that page i see this problem:

The normal/standard drupal menu block shows the following code:

  <ul class="menu"><li class="leaf first active-trail"><a href="/de/content/parentnode" title="parentnode">Parentnode</a></li>
<li class="collapsed"><a href="/de/node/7" title="Node7">Node7</a></li>
<li class="collapsed last"><a href="/de/node/6" title="Node6">Node6</a></li>

</ul> 

That tells me everything with the configuration of Menu Trails is ok (otherwise the parent of the current node wouldn't be set to "active-trail")

But the Code of DPM doesn't show the Active Trail:

  <ul class="dynamic-persistent-menu-menu"><li class=" dynamic-persistent-menu-menu-item" id="dynamic-persistent-menu-menu1686"><a href="/de/content/parentnode" title="parentnode">parentnode</a></li>
<li class=" dynamic-persistent-menu-menu-item" id="dynamic-persistent-menu-menu923"><a href="/de/node/7" title="Node7">Node7</a></li>
<li class=" dynamic-persistent-menu-menu-item" id="dynamic-persistent-menu-menu918"><a href="/de/node/6" title="Node6">Node6</a></li>
</ul><ul class="block dynamic-persistent-menu-sub-menu" id="dynamic-persistent-menu-sub-menu923" style="display:none"><li class=" dynamic-persistent-menu-sub-menu-item" id="dynamic-persistent-menu-sub-menu-item924"><a href="/de/node/8" title="Node8">Node8</a></li>
</ul> 

Am i missing something or is this a bug of DPM?

Best Regards,
Sandro

Comments

pandersb’s picture

subscribing

pandersb’s picture

I was able to fix this, if not in an elegant way, but I modified theme_dynamic_persistent_menu by grabbing some code from menutrails module (taken from function menutrails_get_breadcrumbs()).

The function theme_dynamic_persistent_menu basically iterates through the menu_set_active_trail. And, when you're on a node that is not actually under a parent menu in Drupal's menu system, menu_set_active_trail will not give you anything you can use. So, after figuring out how menutrail determines the node's menu trail, I cobbled them together and it works for me.

/**
 * Theme functions from dynamic_persistent_menu.module
 */

function mytheme_dynamic_persistent_menu($menu_name, $mlid, $timeout) {
  drupal_add_css(drupal_get_path('module', 'dynamic_persistent_menu') .'/dynamic-persistent-menu.css');
  $item_class = "dynamic-persistent-menu-menu-item";
  $sub_item_class = "dynamic-persistent-menu-sub-menu-item";
  
  // Find menu item in the menu tree

  $menu_tree = menu_tree_all_data($menu_name);
  $menu_link = menu_link_load($mlid);
 
  if ($mlid != 0) {
    for ($i=1; $i<10; $i++) {
      foreach($menu_tree as $menu_item) {
        if ($menu_item["link"]['mlid'] == $mlid) {
          $menu = $menu_item['below'];
          break 2;
        }
        else {
          if ($menu_item["link"]['mlid'] == $menu_link['p'.$i]) {
            $menu_tree = $menu_item['below'];
            break;
          }
        }
      }
    }
  }
  else {
    $menu = $menu_tree;
  }

  // Don't display anything if the selected menu has no children

  if (!$menu) {
    return;
  }

  // Backup active menu trail and set a new one

  $active_menu_name = menu_get_active_menu_name();
  menu_set_active_menu_name($menu_name);

  // Build table of mlid in the active trail
  /*
 theme modification: taken from menutrails.module function menutrails_get_breadcrumbs()
  */
 if (arg(0)=='node' && is_numeric(arg(1))) {
		$item = menu_get_item();
		// Give first priority to the selected menu.
		$menutrailsmenu = variable_get('menutrails_menu', FALSE);
		if (!$menutrailsmenu) {
		$menutrailsmenu = db_result(db_query("SELECT menu_name FROM {menu_links} WHERE link_path = '%s' AND module = 'menu'", $item['href']));
		}
		$tree = menu_tree_page_data($menutrailsmenu);
		mytheme_activmenutrails($tree, $item, $trail);
  } else {
  
  /*
  end modification here.
  */
  	foreach (menu_set_active_trail() as $value) {
	    if ($value['mlid']) {
	      $trail[] = $value['mlid'];
	    }
	  }
  }

  
  // Restore active menu trail

  menu_set_active_menu_name($active_menu_name);

  // Build the menus
  
  $output = '<ul class="dynamic-persistent-menu-menu">';

  foreach($menu as $menu_item) {
    $link = $menu_item['link'];
    if ($link['hidden'] == 0) {
      if($link['has_children']) {
        if (is_array($trail) && in_array($link['mlid'], $trail)) {
          $display = 'block';
          $over_menu_default = $link['mlid'];
        }
        else {
          $display = 'none';
        }
      }
      if (is_array($trail) && in_array($link['mlid'], $trail)) {
        $link['in_active_trail'] = TRUE;
      }
      
      $output .= theme('dynamic_persistent_menu_menu_item', $link, 'dynamic-persistent-menu-menu-item', 'dynamic-persistent-menu-menu'.$link['mlid']);
    
      // Submenu
      
      if ($menu_item['link']['has_children'] && is_array($menu_item['below'])) {
        $submenu_items ='';
        foreach($menu_item['below'] as $submenu_item) {
          $link2 = $submenu_item['link'];
          if ($link2['hidden'] == 0) {
            if (is_array($trail) && in_array($link2['mlid'], $trail)) {
              $link2['in_active_trail'] = TRUE;
            }
            $submenu_items .= theme('dynamic_persistent_menu_menu_item', $link2, 'dynamic-persistent-menu-sub-menu-item', 'dynamic-persistent-menu-sub-menu-item'.$link2['mlid']);;
          }
        }
        if ($submenu_items !='') {
          $submenu .= '<ul class="block dynamic-persistent-menu-sub-menu" id="dynamic-persistent-menu-sub-menu'. $link['mlid'] .'" style="display:'. $display  .'">';
          $submenu .= $submenu_items;
          $submenu .= '</ul>';
        }
      }
    }
  }
  $output .= '</ul>';
  $output .= $submenu;
    drupal_add_js("overMenuDefault = 'dynamic-persistent-menu-menu". $over_menu_default ."';", 'inline');
    drupal_add_js("subMenuTimeout = $timeout;", 'inline');
    drupal_add_js(drupal_get_path('module', 'dynamic_persistent_menu') .'/dynamic_persistent_menu.js');
  
  return $output;
}

function mytheme_activmenutrails($tree, $item, &$trail, $above = array()) {
  foreach ($tree as $menu_item) {
    if (!$menu_item['link']['in_active_trail']) {
      continue;
    }
    if ($menu_item['link']['link_path'] == $item['href']) {
      foreach ($above as $trail_item) {
        $trail[] = $trail_item['link']['mlid'];
      }
      $trail[] = $menu_item['link']['mlid'];
      break;
    }
    if (is_array($menu_item['below'])) {
      mytheme_activmenutrails($menu_item['below'], $item, $trail, array_merge($above, array($menu_item)));
    }
  }
}
yrocq’s picture

Status: Active » Needs review

I rewrote the menu loading code. It should work better now. Could you test it? (use the 6.x-1.x-dev snpashot).

yrocq’s picture

Status: Needs review » Fixed

Fixed in 6.x-1.5

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Sinan Erdem’s picture

Is it implemented on version 2? I am using 2.0 alpha 3 and cannot see any active-trail class on the items that have children.

dankh’s picture

Version: 6.x-1.4 » 6.x-1.x-dev
Status: Closed (fixed) » Needs review
StatusFileSize
new800 bytes

Sorry for reopening this but still doesn't work for me. My links doesn't follow any particular scheme so active-trail don't work, but with 2 line jquery fix I made it work. Please check the patch.

dankh’s picture

StatusFileSize
new900 bytes

I found a bug in my first patch here is the update one. I also wanted to explain what my patch does :

- if current menu is active : adds class "dynamic-persistent-menu-active" to the li element
- if sub menu is active : adds class "dynamic-persistent-menu-active" to the top menu li element

This way we can have active trail even if the menu system doesn't detect the parent-children relationship between the menu items.

yrocq’s picture

etcetera9: It isn't fixed in version 2 yet. I want to fix it definitively in 1.x before porting it.

dankh : I don't think that we should do that on the client side. I made some changes in the PHP code that should fix the problem. Could you test it ? ( branch 6.x-1.x-dev).

Thanks !

alberto56’s picture

Status: Needs review » Postponed (maintainer needs more info)

I took over this project from yrocq. I'll wait for someone to respond to yrocq's comment at #9 before pursuing.