1

What is the best practice to change content links based on a class in drupal? I want to have www.example.org transformed to some text based on foo. Should I use hook_view(), or hook_nodeapi()?

1
  • I don't think I understand what you want to do, but manipulating content like that does generally not sound like a great idea.
    – Letharion
    Commented Feb 28, 2012 at 15:38

2 Answers 2

3

You can only use hook_view() when you are implementing the code for the module that defines a content type.

Display a node.

This is a hook used by node modules. It allows a module to define a custom method of displaying its nodes, usually by displaying extra information particular to that node type.

The documentation of hook_view() for Drupal 7 is clearer:

Display a node.

This hook is invoked only on the module that defines the node's content type (use hook_node_view() to act on all node views).

 

If you are writing code in Drupal 6 to alter the content of a content type implemented by another module, you can only use hook_nodeapi('alter'), for which the documentation reports:

"alter": the $node->content array has been rendered, so the node body or teaser is filtered and now contains HTML. This op should only be used when text substitution, filtering, or other raw text operations are necessary.

As what you are trying to achieve is text substitution, this is the hook you should use.

 

The equivalent for Drupal 7 is hook_node_view_alter().

Alter the results of node_view().

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete node content structure has been built.

If the module wishes to act on the rendered HTML of the node rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_node(). See drupal_render() and theme() documentation respectively for details.

1
  • hook_node_view_alter is exactly what I was looking for. Thanks a lot.
    – Jan
    Commented Feb 28, 2012 at 16:50
0

Another option (in Drupal 7) if you're modifying other nodes with your module is to use hook_preprocess_field().

I use it to modify the body of nodes by searching for patterned strings to replace with module widgets. For instance I use |youtube:{VideoID}| to place a video in a node body then use mymodule_preprocess_field() to find and replace the patterns with the appropriate videos.

Not a better solution, just an alternative for modifying nodes your module does not control.

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.