Skip to main content

Query string in drupal menu items

An article from ComputerMinds - Building with Drupal in the UK since 2005
20th Jan 2008

Mike Dixon

Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

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);
}

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.