Has anyone played with adding an input filter for links in content? For my example: I have a persistent query string element that is carried along for all links. So far this is working great, until you get a link in content. For example if you have a link to "/contact" in a node. This isn't run through l() so the persistent url element is dropped.
An input filter (that runs before link processing), which looks for a tags and transforms local links might solve this nicely. Any potential pitfalls here? Performance? A small contrib module might just need to:
- Define an input filter designed to run before link transformation.
- This input filter would scan for any a tags where the link is not absolute/external.
- The href for any of these links would then be transformed as any other link running through l().
Also of note is the internal links module (http://drupal.org/project/intlinks). I was hoping that purl would get called when that filter processed links, but it seems that module just adds titles to the links.
Comments
Comment #1
seanbfuller commentedI spent some time on this and have a partial solution, but I think it is not viable due to performance concerns. Mainly, if we want to apply PURL to internal links in content, then we need to set the input filter cache to false. This causes the entire input filter for a field to be un-cached. This means that all input filtering is re-run for all page requests for logged in users. Anonymous users get a page-level cache, so they do not have this issue.
Here's my code for reference. Things to note:
First the info file:
Next the actual module file (note that I left my dsm calls in there but commented them out):
In doing some quick tests, I didn't notice a huge performance hit. The page timer and memory usage stayed about the same for a logged in user. However, I would think that as the number of concurrent users scale, the impact would get more noticeable.
Next steps
What would be required to make this a viable solution is to be able to cache the input when no internal links are present. I have yet to find a way to do this, but I'm not an expert in working with the input filters. If someone with more knowledge of those systems has a thought on how to make that happen, it would be great to hear.
An alternative method that I want to explore next is to move this into the preprocessor level. That would allow the input filter to be cached, and then add this processing before it gets passed to the theme. I'll be digging into that over the next few days.
Any feedback is appreciated.