I need to use a custom module that replaces the text found in the node body before the node is shown.
Basically, all nodes with the words "Contact Us" in the body field, I want replaced with the actual link to the Contact page.
<?php
print render($page['content']);
?> Before that code in my custom theme page.phl.php file, I need a PHP function/script that lets me use PHP's str_replace on the node's body field, so I can replace specific keywords and phrases before printing the $page['content'].
I have looked and searched around and can't seem to find any examples trying to accomplish this. I'm not sure if the solution is better for a module, or the node.tpl.php file, but I need the ability to replace strings from the body field of my nodes. Any help whatsoever would be super appreciated!
Comments
Custom Filter module
I use a module called Custom Filter for this kind of thing on my site. It enables you to set up simple regular expressions to find specific characters in your content and replace them on output with something else.
You can see it in action for example on one of my site's tutorial pages: Photoshop Inset Buttons ... in my tutorials, I have tooltips that show when you hover over various tool names. Instead of typing out all the HTML for these over and over, I set it up so I can simply type something like
[Ellipse]and when the page renders it replaces that with the appropriate HTML. In this case I surround my text with brackets to ensure that the filter only applies to the word when I specifically intend it to. Another instance on this page is the»ascii character I use between menu names. Instead of typing out the ascii every time or copy/pasting the character, I simply type >> and it replaces it for me on output.Anyhow, hope this helps :)
A couple ideas
Hi,
One solution is implement a custom filter. See an example here: http://api.drupal.org/api/examples/filter_example!filter_example.module/function/_filter_example_filter_foo_process/7.
I wouldn't use the Theme layer. I would extend the Drupal core with a module. A Module creation tutorial is located here: http://drupal.org/node/1074360
You could alternatively implement hook_node_view(). Docs here: http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_view/7. Example here http://api.drupal.org/api/examples/nodeapi_example!nodeapi_example.module/function/nodeapi_example_node_view/7
That should get you started.
Hope that helps :)
Good Luck