Author image
Senior Mind

Query string in drupal menu items

This was written in response to a post on drupal.org regarding query strings in drupal menu items. Currently if you create a drupal menu item linking to an external site with a querystring then this will render correctly. However if you try and use a relative URL (i.e. an internal link) then your querystring will be escaped and the URL won't work.

This override lives in your template.php file and parses all your menu paths for internal links, and will pass query strings into the l function correctly ...

function phptemplate_menu_item_link($item, $link_item) {
  if (!$item['query']){
    $parsed_url = parse_url($item['path']);
    if ($parsed_url['query'] && !$parsed_url['host']){
      $item['query']=$parsed_url['query'];
      $link_item['path']=$parsed_url['path'];
    }
  }
  return l($item['title'],
           $link_item['path'],
           !empty($item['description']) ? array('title' => $item['description']) : array(),
           isset($item['query']) ? $item['query'] : NULL);
}