Is there a way to find certain types of links and edit them from a custom module in Drupal 7? I want to add an attribute to all pdf links throughout my site. I tried the following:

function my_module_preprocess_link(&$link){
  if( substr($link['path'], 4) === '.php' ) {

But I suppose since the pdf links are contained in the node content of my pages, they aren't included here.

I'd like to add a custom class to all pdf links or maybe an inline onclick javascript event. Is there a way to do that?


It depends how the links have been created.

If the links were added using a field, eg using the file module, then you could look to that module for it's theme functions, and create your own replacement theme function in your module or theme.

If (just reread your question) the links are included in the body (or other textarea field) of your site's nodes, then you would have to rely on an input filter to alter them before displaying the field. You could look to a module like ELF, for how this can be accomplished using an input filter, or 'External Links' for an example using javascript.

| improve this answer | |
  • Thanks, the links are indeed located in the body of the nodes. I played with preprocessing the node's content, but I found the body content in multiple parts of the node array and couldn't figure it out. Since I basically wanted to add a click event, I ended up just including a js file with $('body').on('click', 'a', function(){... – Chris Dec 10 '14 at 14:45
  • That sounds a perfectly reasonable approach. Accessing the node body should be pretty easy if you need to do that again, though I would suggest that using an input filter might be more drupally (and performative) than preprocessing the node directly. It would require nodes to be bulk re-saved before existing nodes updated though. – autopoietic Dec 10 '14 at 14:56
  • $node->body['und'][0]['value'] – autopoietic Dec 10 '14 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.