25 August 2014

There are many functions for menus, but since Drupal is not hierarchical but is modular, that's why menu doesn't have node requirement and the opposite way around. So this snippet shows how to get node menu item (mlid) by node id (nid) in Drupal 7. The fastest way to do it is to run a simple query.

Source code viewer
  1. $mlid = reset(db_select('menu_links' , 'ml')
  2. ->condition('ml.link_path' , 'node/' . $nid)
  3. ->condition('ml.menu_name', $menu_name)
  4. ->fields('ml' , array('mlid'))
  5. ->execute()
  6. ->fetchCol());
  8. if (!empty($mild)) {
  9. // Use your menu link id (mlid) wisely...
  10. }
Programming Language: PHP