Well, as my title says, is it possible to change the way that links behave in Drupal?

I have some external webpages that must use a relative path such as http://example.com/testlink/test.htm instead of http://example.com/index.php?q=testlink/test.htm

i have been trying to change this in my template.php with the following code. It seems that the "path" is correct (tried with printing it) althought I believe that the $link_item might have something to do with the problem.
When I create my link in the menu module I use /testlink/test.htm and then it changes my link to an internal node (which I DONT want)
All ideas is accepted!!

function dg2k_menu_item_link($item, $link_item) {
  if ((substr($link_item['path'], 0, 4) == 'http')) {
    return l($item['title'], $link_item['path'], isset($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'));
  }
  if ((substr($link_item['path'], 0, 1) == '/')) {
    return l($item['title'], $link_item['path'], isset($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'));
  }
  else {
    return l($item['title'], $link_item['path'], isset($item['description']) ? array('title' => $item['description']) : array());
  }
}

Comments

cybertron1’s picture

I have solved it!!

I did it like this:

function dg2k_menu_item_link($item, $link_item) {
 $path = 'http://' . variable_get('$_SITE', 'drupal');
  if ((substr($link_item['path'], 0, 4) == 'http')) {
    return l($item['title'], $link_item['path'], isset($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'));
  }
  if ((substr($link_item['path'], 0, 1) == '/')) {
    return l($item['title'], t($path . t($item['path'])), isset($item['description']) ? array('title' => $item['description'], 'target' => '_blank') : array('target' => '_blank'));
  }
  else {
    return l($item['title'], $link_item['path'], isset($item['description']) ? array('title' => $item['description']) : array());
  }
}
cybertron1’s picture

Hi!

Now it has come to the fact that I need to upgrade my systems to drupal 6.x
and the above function doesn't work any more. Does anyone know how to correct that?

thanks in advance.

i have come this far:

function dg_zen_2009_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET
  if(TRUE) {
  if(substr($link['path'], 0, 4) == 'http') {
      $link['localized_options']['attributes']['target'] = '_blank';
        
    }
  }
  return l($link['title'], $link['href'], $link['localized_options']);
}
cybertron1’s picture

function dg_zen_2009_menu_item_link($link) {
 $path = 'http://' . $_SERVER['SERVER_NAME'];
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  // DO CHECKS ON LINKS TO GENERATE THE TARGET

  if((substr($link['href'], 0, 4) == 'http')) {
      $link['localized_options']['attributes']['target'] = '_blank';
  }
    if((substr($link['href'], 0, 1) == '/')) {
      $link['localized_options']['attributes']['target'] = '_blank';
      $link['href'] = t($path) . $link['href'];
  }
  
      if((substr($link['href'], 0, 4) == 'file')) {
      $link['localized_options']['attributes']['target'] = '_blank';
     
  }