phpdrupal-7

Custom filter with hook_filter_info() in Drupal


I got a problem when trying to create a custom filter with hook_filter_info(). I have to make some replacement based on the fields of current viewed node. I am simply using the following code.

return str_replace('%people1%', 'REPLACING WORKING', $text);

In the process callback, that code works fine, but I can't get the current node ID (menu_get_item() or arg()). Also, I have to clear the cache every time I make some changes ('cache' => FALSE).

Is there anything I need to know about processing data in the process callback?


Solution

  • menu_get_item() and arg() don't return a different value when called from a process callback of a input filter; they are not returning the value you are expecting, probably because the input filter is called in a different context than you think.

    An input filter is called when:

    Even in the case the input filter is used to render the body of a node, there could still be two cases:

    A module implementing an input filter cannot understand for which entity it is called, and the path of the current page doesn't necessarily give that information. For example, I could have a module that at mymodule/disclaimer shows the content of the node with ID equal to 2, or I could have a view that at example_path/just_to_make_an_example shows the excerpt taken from nodes matching some criteria I decided.

    If you need to replace some words in a node body, for example replacing %author% with the username of the node creator, you should rather implement hook_node_view_alter().