The background to this is a small site where the site owner is adding links in the form www.sitename.com to filtered HTML text. Drupal does a great job of recognizing these and converting them into "a" tags.

However, these links take the visitor away from his site, so I'd like them to open a tab / window. I've dug down into the code and found the drupal/modules/filter/filter.module file and within it the _filter_url_parse_full_links and _filter_url_parse_partial_links functions.

I changed them to add target attributes as follows:

/**
 * Make links out of absolute URLs.
 */
function _filter_url_parse_full_links($match) {
  $match[2] = decode_entities($match[2]);
  $caption = check_plain(_filter_url_trim($match[2]));
  $match[2] = check_url($match[2]);
  return $match[1] . '<ahref="'. $match[2] .'" title="'. $match[2] .'" target=\"_blank\">'. $caption .'</a>'. $match[5];
}

/**
 * Make links out of domain names starting with "www."
 */
function _filter_url_parse_partial_links($match) {
  $match[2] = decode_entities($match[2]);
  $caption = check_plain(_filter_url_trim($match[2]));
  $match[2] = check_plain($match[2]);
  return $match[1] . '<ahref="http://'. $match[2] .'" title="'. $match[2] .'" target=\"_blank\">'. $caption .'</a>'. $match[3];
}

However, these changes seem to be completely ignored. I've double checked the code on the server and the changes are definitely there, but they aren't reflected in the HTML being generated.

So, am I in completely the wrong place? or am I missing something silly and obvious?

All help and ideas appreciated.

Comments

kuroi’s picture

The above code had some small syntax errors. Specifically the double quotes that I had inserted didn't need to be escaped.

However, it appears that the changes to the filters don't take effect until the text in the relevant block is also updated. This suggests to me that the generated and parsed text is cached. Have I understood this correctly?

Also, is there a way of over-riding, rather than over-writing, these functions? They don't appear to be over-rideable as part of a theme.

StuartMackenzie’s picture

have you tried the external link module? I use it on my site and it works brilliantly for me! far easier than banging away at code. If this isn't an option for you then look at adding something to a template.php file in your theme rather than hacking the core code. Lots of functions in drupal can be overriden in your own module or theme files rather than actually hacking the original code, which may cause you problems when updating or upgrading a site later on.

_ _ _ _ _

Stuart Mackenzie - rojojam.com
- slow like snail, furry like panda with the roar of a donkey!
- also trying to learn the dark arts of drupal

kuroi’s picture

Hi Stuart

Thanks for the response. I did consider the external link module, but having been very careful not to rely on javascript across the rest of the site, didn't want to fall at the final hurdle. My understanding was that only theme functions can be over-ridden in the template.php file, which is why I ended up patching the core code instead. This is never something that I would do if I had an alternative, so I am very interested in your comment that module functions can be over-ridden too. I hadn't been able to find anything on this. Please can you tell me more or give an example.

StuartMackenzie’s picture

some can and some can't as far as I can remember (sorry I'm stuck at work and not very useful). I'd need to trawl thru books and handbook, I'm no coder.

I still think for the minority of people with javascript not running staying within the same browser window external links would be a better call than hacking core but it is your choice to make I guess!

One thing that may be worth trying is to make a module for your own input format you've already found moast of the code you need to do it, just lift and it and make it your own!

_ _ _ _ _

Stuart Mackenzie - rojojam.com
- slow like snail, furry like panda with the roar of a donkey!
- also trying to learn the dark arts of drupal