4

I am a newbie and have scoured forums (including this one), books, and documentation but cannot find a clear answer to this:

What is the best method (custom module? PHP within node content? other?) to replace a token within the body of a node with the value of a custom user profile field?

Use case: I need to replace text within an external URL with a string specific to the current user. I'm hoping I can put a token string into the URL within the node content, and have the token replaced with the specified value from the current user's profile.

I have not created any custom modules, but am willing to dive into it if somebody recommends a specific path to take.

5

Token Filter

Token Filter is a very simple module to make token values available as an input filter.

2

To implement a system to replace custom tokens, you have two possibilities, supposing that you are referring to text present in the node body that needs to be replaced with dynamic values:

  • use a filter module that allows you to define which text needs to be replaced, and with what
  • use a custom module that replaces the text found in the node body before the node is shown

In the first case, the filter module would be invoked also for comments, and the filter would not be able to make a difference between replacing text in a node body, or in a comment body. In the case the module allows you to define the PHP code executed when a specific text is found in the node/comment body, there is also a performance issue. Instead of passing PHP code to eval(), it would be better to define a custom module that implements a specific filter to replace a specific text you need. Also in this case, the module would not be able to know when it is invoked for a node or a comment; it also means the filter module could not get information outside the arguments given to the filter callback, which means it is not able to get information about the author of the node, since the filter callback doesn't get any node or comment object.
In the case of Custom filter, the description given for the input format is not translatable; that means that passing from English to French as language for the site (or the node being shown), the description reported would not be shown in French.

In the latter case, a module implementing hook_nodeapi('view'), in Drupal 6, could alter the content of $node->content to alter the node body; similarly, a module implementing hook_node_view() would achieve the same result in Drupal 7 and later versions.
In both the cases, $node->content['body'] contains the node body as array that follows the format used by the form API, or required by drupal_render().

Enabling the PHP input format for a site that is not used as developing/testing site is not considered a good idea. Rather than enabling that input format is better to implement a custom module, which is what has been done on Drupal.org that replaced a content type using the PHP input format with a normal content type, and implemented a custom module to alter the content of specific nodes.

  • Thank you very much for your clear and detailed answer, kiamlaluno! I'm using Drupal 7 - forgot to mention in my original question. I'm investigating the Custom Filter module, but because the token I'm trying to replace is a custom user field, I think I'll have to go the custom module route. Exploring that now. Great to have your advice about PHP input format too. – ctlarsen May 30 '11 at 17:19
  • I apologize, I'm pretty confused. I've tried many verbatim and different combinations of $node->content['body'] but can't seem to change the body content using hook_node_view in my module. I can't assign text to $node->content['body']['#value'] and have it show up. Does the input format matter? Permissions? I'm pretty stuck. Thanks for any advice! – ctlarsen May 30 '11 at 23:44
  • Never mind, I figured it out. I can read and write body content in a hook_node_view implementation using "$node->content['body'][0]['#markup']". Thanks. – ctlarsen May 31 '11 at 1:35
1

You might be interested in Custom filter.

Instead of creating a new filter module for every filter you need, you can use this module for tasks like creating your own tags, replacing token values and all changes you need in the text. You can use simple replacements or use PHP code to write more powerful things (like use it to highligth code with Geshi).

  • Thanks for your quick response, ha5bro! I am investigating this module now. – ctlarsen May 30 '11 at 17:12

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.