I'm currently working on a custom module that has a filter that requires the node object for the output.

Originally the filter was simply pulling in the NID from arg(1) and using that to load in the node object, but unfortunately if the filter cache is flushed and the nodes rebuild their output from anywhere other than the node page itself arg(1) will not contain the NID and therefore the filter will break.

I'm considering putting the NID into the token that the filter converts, but obviously the NID would not be known during the creation of a node. It wouldn't be overly difficult to alter the token via hook_nodeapi() to insert the NID, but it seems like a lot of hastle.

Is there a simpler way to provide a filter with some sort of context so it can be aware of which node invoked it?

2 Answers 2


Filters are not context-aware and AFAIK, there is no way at the moment to do what you want. This has been a feature request since 2008, but the proposed patch didn't make it into Drupal 7.

If this is very important to the site you're building, you can consider patching Drupal core. However, once Drupal core is patched, you will need to take extra care (and time) when updating your site.

  • It's for a contrib module, so that's not really a solution. I understand it's not context aware, but I'm curious if people have an out of the box solution.
    – Decipher
    Mar 7, 2011 at 9:58
  • Ok, I see. Patching core is obviously no option for a contrib module. Your hook_nodeapi idea may not be the cleanest way to do this, but in fact it's the best workaround I've heard of so far. Mar 7, 2011 at 10:08

Filter modules are invoked also for comments, but none of the arguments passed to the filter allows it to understand when it is invoked to alter a node content, or a comment.

You can implement hook_nodeapi() to change the content of a node when it is being assembled before rendering; check the value of $op is "view".
This is actually what a third-party module that alters the content of a node basing on the content of some node fields does.

If the string the module is replacing can be entered by the users who edit the node (and the users needs to know this possibility), you also need to alter, for example, the description given for the node body field in the edit form to include a description of the "tokens" the module is replacing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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