Get the menu id from a path alias

Sometimes we need the menu id (mlid) that is attached to a path alias. Here is some code that gives you the mlid.

 

function _get_mlid_from_alias($path = NULL){
  if($path == NULL){
    return FALSE;
  }
  $mlid = array();

  $q = drupal_get_normal_path($path);

  $menu_info = db_select('menu_links' , 'ml')
    ->condition('ml.link_path' , $q)
    ->fields('ml', array('mlid', 'plid'))
    ->execute()
    ->fetchAll();
  foreach($menu_info as $key => $value) {
    $mlid[] = $menu_info[$key]->mlid;
  }
  return $mlid;
}

Add new comment